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.
Files changed (267) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/distill/propose.d.ts +10 -0
  3. package/dist/distill/propose.d.ts.map +1 -1
  4. package/dist/distill/propose.js +49 -10
  5. package/dist/distill/propose.js.map +1 -1
  6. package/dist/distill/state.d.ts +6 -0
  7. package/dist/distill/state.d.ts.map +1 -1
  8. package/dist/distill/state.js +1 -1
  9. package/dist/distill/state.js.map +1 -1
  10. package/dist/extract/index.d.ts +16 -0
  11. package/dist/extract/index.d.ts.map +1 -0
  12. package/dist/extract/index.js +95 -0
  13. package/dist/extract/index.js.map +1 -0
  14. package/dist/extract/normalize.d.ts +7 -0
  15. package/dist/extract/normalize.d.ts.map +1 -0
  16. package/dist/extract/normalize.js +52 -0
  17. package/dist/extract/normalize.js.map +1 -0
  18. package/dist/extract/office.d.ts +63 -0
  19. package/dist/extract/office.d.ts.map +1 -0
  20. package/dist/extract/office.js +763 -0
  21. package/dist/extract/office.js.map +1 -0
  22. package/dist/extract/pdf.d.ts +16 -0
  23. package/dist/extract/pdf.d.ts.map +1 -0
  24. package/dist/extract/pdf.js +196 -0
  25. package/dist/extract/pdf.js.map +1 -0
  26. package/dist/extract/types.d.ts +55 -0
  27. package/dist/extract/types.d.ts.map +1 -0
  28. package/dist/extract/types.js +47 -0
  29. package/dist/extract/types.js.map +1 -0
  30. package/dist/extract/worker.d.ts +2 -0
  31. package/dist/extract/worker.d.ts.map +1 -0
  32. package/dist/extract/worker.js +57 -0
  33. package/dist/extract/worker.js.map +1 -0
  34. package/dist/integrations/distill.d.ts.map +1 -1
  35. package/dist/integrations/distill.js +1 -0
  36. package/dist/integrations/distill.js.map +1 -1
  37. package/dist/integrations/engine.d.ts +208 -2
  38. package/dist/integrations/engine.d.ts.map +1 -1
  39. package/dist/integrations/engine.js +360 -30
  40. package/dist/integrations/engine.js.map +1 -1
  41. package/dist/integrations/google.d.ts +2 -1
  42. package/dist/integrations/google.d.ts.map +1 -1
  43. package/dist/integrations/google.js +13 -117
  44. package/dist/integrations/google.js.map +1 -1
  45. package/dist/integrations/http-json.d.ts +27 -0
  46. package/dist/integrations/http-json.d.ts.map +1 -0
  47. package/dist/integrations/http-json.js +148 -0
  48. package/dist/integrations/http-json.js.map +1 -0
  49. package/dist/integrations/microsoft.d.ts +22 -0
  50. package/dist/integrations/microsoft.d.ts.map +1 -0
  51. package/dist/integrations/microsoft.js +1780 -0
  52. package/dist/integrations/microsoft.js.map +1 -0
  53. package/dist/integrations/microsoft.ui.d.ts +18 -0
  54. package/dist/integrations/microsoft.ui.d.ts.map +1 -0
  55. package/dist/integrations/microsoft.ui.js +131 -0
  56. package/dist/integrations/microsoft.ui.js.map +1 -0
  57. package/dist/integrations/oauth.d.ts.map +1 -1
  58. package/dist/integrations/oauth.js +10 -2
  59. package/dist/integrations/oauth.js.map +1 -1
  60. package/dist/integrations/queue.d.ts +1 -1
  61. package/dist/integrations/queue.d.ts.map +1 -1
  62. package/dist/integrations/queue.js +23 -1
  63. package/dist/integrations/queue.js.map +1 -1
  64. package/dist/integrations/routes.d.ts +26 -0
  65. package/dist/integrations/routes.d.ts.map +1 -1
  66. package/dist/integrations/routes.js +525 -11
  67. package/dist/integrations/routes.js.map +1 -1
  68. package/dist/integrations/runtime.d.ts +14 -2
  69. package/dist/integrations/runtime.d.ts.map +1 -1
  70. package/dist/integrations/runtime.js +51 -11
  71. package/dist/integrations/runtime.js.map +1 -1
  72. package/dist/integrations/state.d.ts +1 -1
  73. package/dist/integrations/state.d.ts.map +1 -1
  74. package/dist/integrations/state.js +74 -5
  75. package/dist/integrations/state.js.map +1 -1
  76. package/dist/integrations/types.d.ts +84 -1
  77. package/dist/integrations/types.d.ts.map +1 -1
  78. package/dist/integrations/types.js +21 -1
  79. package/dist/integrations/types.js.map +1 -1
  80. package/dist/okf/import.d.ts.map +1 -1
  81. package/dist/okf/import.js +208 -65
  82. package/dist/okf/import.js.map +1 -1
  83. package/dist/okf/index.d.ts.map +1 -1
  84. package/dist/okf/index.js +5 -0
  85. package/dist/okf/index.js.map +1 -1
  86. package/dist/okf/map.d.ts.map +1 -1
  87. package/dist/okf/map.js +3 -2
  88. package/dist/okf/map.js.map +1 -1
  89. package/dist/search/watcher.d.ts.map +1 -1
  90. package/dist/search/watcher.js +3 -22
  91. package/dist/search/watcher.js.map +1 -1
  92. package/dist/serve/index.d.ts.map +1 -1
  93. package/dist/serve/index.js +7 -0
  94. package/dist/serve/index.js.map +1 -1
  95. package/dist/sleep/index.js +2 -2
  96. package/dist/sleep/report.js +1 -1
  97. package/dist/sleep/report.js.map +1 -1
  98. package/dist/sleep/tension-scan.d.ts.map +1 -1
  99. package/dist/sleep/tension-scan.js +0 -0
  100. package/dist/sleep/tension-scan.js.map +1 -1
  101. package/dist/storage/local.d.ts +1 -0
  102. package/dist/storage/local.d.ts.map +1 -1
  103. package/dist/storage/local.js +8 -1
  104. package/dist/storage/local.js.map +1 -1
  105. package/dist/utils/config.d.ts +8 -1
  106. package/dist/utils/config.d.ts.map +1 -1
  107. package/dist/utils/config.js +143 -18
  108. package/dist/utils/config.js.map +1 -1
  109. package/dist/view/pages.d.ts.map +1 -1
  110. package/dist/view/pages.js +11 -0
  111. package/dist/view/pages.js.map +1 -1
  112. package/dist/view/render.d.ts.map +1 -1
  113. package/dist/view/render.js +37 -2
  114. package/dist/view/render.js.map +1 -1
  115. package/package.json +11 -4
  116. package/src/integrations/microsoft/assets/VENDORING.md +109 -0
  117. package/src/integrations/microsoft/assets/glue.js +244 -0
  118. package/src/integrations/microsoft/assets/picker-serializer.js +99 -0
  119. package/src/integrations/microsoft/assets/vendor/msal-browser/app/IPublicClientApplication.mjs +91 -0
  120. package/src/integrations/microsoft/assets/vendor/msal-browser/app/PublicClientApplication.mjs +355 -0
  121. package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/NativeStatusCodes.mjs +15 -0
  122. package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/PlatformAuthDOMHandler.mjs +153 -0
  123. package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/PlatformAuthExtensionHandler.mjs +271 -0
  124. package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/PlatformAuthProvider.mjs +95 -0
  125. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/AccountManager.mjs +49 -0
  126. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/AsyncMemoryStorage.mjs +144 -0
  127. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/BrowserCacheManager.mjs +1465 -0
  128. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/CacheHelpers.mjs +45 -0
  129. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/CacheKeys.mjs +32 -0
  130. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/CookieStorage.mjs +94 -0
  131. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/DatabaseStorage.mjs +208 -0
  132. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/EncryptedData.mjs +13 -0
  133. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/LocalStorage.mjs +319 -0
  134. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/MemoryStorage.mjs +48 -0
  135. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/SessionStorage.mjs +46 -0
  136. package/src/integrations/microsoft/assets/vendor/msal-browser/cache/TokenCache.mjs +220 -0
  137. package/src/integrations/microsoft/assets/vendor/msal-browser/config/Configuration.mjs +138 -0
  138. package/src/integrations/microsoft/assets/vendor/msal-browser/controllers/NestedAppAuthController.mjs +483 -0
  139. package/src/integrations/microsoft/assets/vendor/msal-browser/controllers/StandardController.mjs +1532 -0
  140. package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/BrowserCrypto.mjs +354 -0
  141. package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/CryptoOps.mjs +146 -0
  142. package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/PkceGenerator.mjs +63 -0
  143. package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/SignedHttpRequest.mjs +50 -0
  144. package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/TokenBindingKeyManager.mjs +156 -0
  145. package/src/integrations/microsoft/assets/vendor/msal-browser/encode/Base64Decode.mjs +43 -0
  146. package/src/integrations/microsoft/assets/vendor/msal-browser/encode/Base64Encode.mjs +47 -0
  147. package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserAuthError.mjs +26 -0
  148. package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserAuthErrorCodes.mjs +65 -0
  149. package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserConfigurationAuthError.mjs +24 -0
  150. package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserConfigurationAuthErrorCodes.mjs +11 -0
  151. package/src/integrations/microsoft/assets/vendor/msal-browser/error/NativeAuthError.mjs +79 -0
  152. package/src/integrations/microsoft/assets/vendor/msal-browser/error/NativeAuthErrorCodes.mjs +12 -0
  153. package/src/integrations/microsoft/assets/vendor/msal-browser/error/NestedAppAuthError.mjs +25 -0
  154. package/src/integrations/microsoft/assets/vendor/msal-browser/event/EventHandler.mjs +110 -0
  155. package/src/integrations/microsoft/assets/vendor/msal-browser/event/EventMessage.mjs +60 -0
  156. package/src/integrations/microsoft/assets/vendor/msal-browser/event/EventType.mjs +30 -0
  157. package/src/integrations/microsoft/assets/vendor/msal-browser/index.mjs +48 -0
  158. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/BaseInteractionClient.mjs +150 -0
  159. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/HybridSpaAuthorizationCodeClient.mjs +16 -0
  160. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/PlatformAuthInteractionClient.mjs +734 -0
  161. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/PopupClient.mjs +528 -0
  162. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/RedirectClient.mjs +464 -0
  163. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentAuthCodeClient.mjs +85 -0
  164. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentCacheClient.mjs +58 -0
  165. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentIframeClient.mjs +255 -0
  166. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentRefreshClient.mjs +77 -0
  167. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/StandardInteractionClient.mjs +217 -0
  168. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_handler/InteractionHandler.mjs +95 -0
  169. package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_handler/SilentHandler.mjs +71 -0
  170. package/src/integrations/microsoft/assets/vendor/msal-browser/naa/BridgeError.mjs +11 -0
  171. package/src/integrations/microsoft/assets/vendor/msal-browser/naa/BridgeProxy.mjs +152 -0
  172. package/src/integrations/microsoft/assets/vendor/msal-browser/naa/BridgeStatusCode.mjs +18 -0
  173. package/src/integrations/microsoft/assets/vendor/msal-browser/naa/mapping/NestedAppAuthAdapter.mjs +239 -0
  174. package/src/integrations/microsoft/assets/vendor/msal-browser/navigation/NavigationClient.mjs +47 -0
  175. package/src/integrations/microsoft/assets/vendor/msal-browser/network/FetchClient.mjs +153 -0
  176. package/src/integrations/microsoft/assets/vendor/msal-browser/operatingcontext/BaseOperatingContext.mjs +112 -0
  177. package/src/integrations/microsoft/assets/vendor/msal-browser/operatingcontext/NestedAppOperatingContext.mjs +79 -0
  178. package/src/integrations/microsoft/assets/vendor/msal-browser/operatingcontext/StandardOperatingContext.mjs +50 -0
  179. package/src/integrations/microsoft/assets/vendor/msal-browser/packageMetadata.mjs +7 -0
  180. package/src/integrations/microsoft/assets/vendor/msal-browser/popup_relay/constants.mjs +20 -0
  181. package/src/integrations/microsoft/assets/vendor/msal-browser/popup_relay/relayClient.mjs +113 -0
  182. package/src/integrations/microsoft/assets/vendor/msal-browser/protocol/Authorize.mjs +326 -0
  183. package/src/integrations/microsoft/assets/vendor/msal-browser/request/RequestHelpers.mjs +143 -0
  184. package/src/integrations/microsoft/assets/vendor/msal-browser/response/ResponseHandler.mjs +45 -0
  185. package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserPerformanceClient.mjs +126 -0
  186. package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserPerformanceEvents.mjs +133 -0
  187. package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserPerformanceMeasurement.mjs +96 -0
  188. package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserRootPerformanceEvents.mjs +53 -0
  189. package/src/integrations/microsoft/assets/vendor/msal-browser/utils/BrowserConstants.mjs +220 -0
  190. package/src/integrations/microsoft/assets/vendor/msal-browser/utils/BrowserProtocolUtils.mjs +27 -0
  191. package/src/integrations/microsoft/assets/vendor/msal-browser/utils/BrowserUtils.mjs +364 -0
  192. package/src/integrations/microsoft/assets/vendor/msal-browser/utils/Helpers.mjs +19 -0
  193. package/src/integrations/microsoft/assets/vendor/msal-browser/utils/MsalFrameStatsUtils.mjs +38 -0
  194. package/src/integrations/microsoft/assets/vendor/msal-browser/webBrokerBridge/PendingRequestRegistry.mjs +63 -0
  195. package/src/integrations/microsoft/assets/vendor/msal-browser/webBrokerBridge/WebBrokerBridgeError.mjs +38 -0
  196. package/src/integrations/microsoft/assets/vendor/msal-browser/webBrokerBridge/WebBrokerBridgeErrorMap.mjs +57 -0
  197. package/src/integrations/microsoft/assets/vendor/msal-common/account/AccountInfo.mjs +91 -0
  198. package/src/integrations/microsoft/assets/vendor/msal-common/account/AuthToken.mjs +70 -0
  199. package/src/integrations/microsoft/assets/vendor/msal-common/account/CcsCredential.mjs +12 -0
  200. package/src/integrations/microsoft/assets/vendor/msal-common/account/ClientInfo.mjs +43 -0
  201. package/src/integrations/microsoft/assets/vendor/msal-common/account/TokenClaims.mjs +24 -0
  202. package/src/integrations/microsoft/assets/vendor/msal-common/authority/Authority.mjs +988 -0
  203. package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityFactory.mjs +41 -0
  204. package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityMetadata.mjs +158 -0
  205. package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityOptions.mjs +20 -0
  206. package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityType.mjs +16 -0
  207. package/src/integrations/microsoft/assets/vendor/msal-common/authority/CloudInstanceDiscoveryErrorResponse.mjs +12 -0
  208. package/src/integrations/microsoft/assets/vendor/msal-common/authority/CloudInstanceDiscoveryResponse.mjs +12 -0
  209. package/src/integrations/microsoft/assets/vendor/msal-common/authority/OpenIdConfigResponse.mjs +14 -0
  210. package/src/integrations/microsoft/assets/vendor/msal-common/authority/ProtocolMode.mjs +26 -0
  211. package/src/integrations/microsoft/assets/vendor/msal-common/authority/RegionDiscovery.mjs +116 -0
  212. package/src/integrations/microsoft/assets/vendor/msal-common/cache/CacheManager.mjs +1202 -0
  213. package/src/integrations/microsoft/assets/vendor/msal-common/cache/persistence/TokenCacheContext.mjs +29 -0
  214. package/src/integrations/microsoft/assets/vendor/msal-common/cache/utils/AccountEntityUtils.mjs +222 -0
  215. package/src/integrations/microsoft/assets/vendor/msal-common/cache/utils/CacheHelpers.mjs +294 -0
  216. package/src/integrations/microsoft/assets/vendor/msal-common/client/AuthorizationCodeClient.mjs +289 -0
  217. package/src/integrations/microsoft/assets/vendor/msal-common/client/RefreshTokenClient.mjs +262 -0
  218. package/src/integrations/microsoft/assets/vendor/msal-common/client/SilentFlowClient.mjs +142 -0
  219. package/src/integrations/microsoft/assets/vendor/msal-common/config/ClientConfiguration.mjs +111 -0
  220. package/src/integrations/microsoft/assets/vendor/msal-common/constants/AADServerParamKeys.mjs +74 -0
  221. package/src/integrations/microsoft/assets/vendor/msal-common/crypto/DpopProofGenerator.mjs +152 -0
  222. package/src/integrations/microsoft/assets/vendor/msal-common/crypto/ICrypto.mjs +52 -0
  223. package/src/integrations/microsoft/assets/vendor/msal-common/crypto/ITokenBindingKeyManager.mjs +27 -0
  224. package/src/integrations/microsoft/assets/vendor/msal-common/crypto/JoseHeader.mjs +72 -0
  225. package/src/integrations/microsoft/assets/vendor/msal-common/crypto/PopTokenGenerator.mjs +109 -0
  226. package/src/integrations/microsoft/assets/vendor/msal-common/error/AuthError.mjs +31 -0
  227. package/src/integrations/microsoft/assets/vendor/msal-common/error/AuthErrorCodes.mjs +13 -0
  228. package/src/integrations/microsoft/assets/vendor/msal-common/error/CacheError.mjs +44 -0
  229. package/src/integrations/microsoft/assets/vendor/msal-common/error/CacheErrorCodes.mjs +10 -0
  230. package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientAuthError.mjs +26 -0
  231. package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientAuthErrorCodes.mjs +46 -0
  232. package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientConfigurationError.mjs +23 -0
  233. package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientConfigurationErrorCodes.mjs +38 -0
  234. package/src/integrations/microsoft/assets/vendor/msal-common/error/InteractionRequiredAuthError.mjs +73 -0
  235. package/src/integrations/microsoft/assets/vendor/msal-common/error/InteractionRequiredAuthErrorCodes.mjs +53 -0
  236. package/src/integrations/microsoft/assets/vendor/msal-common/error/JoseHeaderError.mjs +24 -0
  237. package/src/integrations/microsoft/assets/vendor/msal-common/error/JoseHeaderErrorCodes.mjs +12 -0
  238. package/src/integrations/microsoft/assets/vendor/msal-common/error/NetworkError.mjs +34 -0
  239. package/src/integrations/microsoft/assets/vendor/msal-common/error/PlatformBrokerError.mjs +48 -0
  240. package/src/integrations/microsoft/assets/vendor/msal-common/error/ServerError.mjs +22 -0
  241. package/src/integrations/microsoft/assets/vendor/msal-common/index-browser.mjs +79 -0
  242. package/src/integrations/microsoft/assets/vendor/msal-common/logger/Logger.mjs +291 -0
  243. package/src/integrations/microsoft/assets/vendor/msal-common/network/INetworkModule.mjs +20 -0
  244. package/src/integrations/microsoft/assets/vendor/msal-common/network/RequestThumbprint.mjs +27 -0
  245. package/src/integrations/microsoft/assets/vendor/msal-common/network/ThrottlingUtils.mjs +91 -0
  246. package/src/integrations/microsoft/assets/vendor/msal-common/packageMetadata.mjs +7 -0
  247. package/src/integrations/microsoft/assets/vendor/msal-common/protocol/Authorize.mjs +239 -0
  248. package/src/integrations/microsoft/assets/vendor/msal-common/protocol/Token.mjs +148 -0
  249. package/src/integrations/microsoft/assets/vendor/msal-common/request/AuthenticationHeaderParser.mjs +63 -0
  250. package/src/integrations/microsoft/assets/vendor/msal-common/request/BaseAuthRequest.mjs +37 -0
  251. package/src/integrations/microsoft/assets/vendor/msal-common/request/RequestParameterBuilder.mjs +529 -0
  252. package/src/integrations/microsoft/assets/vendor/msal-common/request/ScopeSet.mjs +204 -0
  253. package/src/integrations/microsoft/assets/vendor/msal-common/response/ResponseHandler.mjs +432 -0
  254. package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/PerformanceClient.mjs +557 -0
  255. package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/PerformanceEvent.mjs +68 -0
  256. package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/PerformanceEvents.mjs +73 -0
  257. package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/StubPerformanceClient.mjs +61 -0
  258. package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/server/ServerTelemetryManager.mjs +297 -0
  259. package/src/integrations/microsoft/assets/vendor/msal-common/url/UrlString.mjs +164 -0
  260. package/src/integrations/microsoft/assets/vendor/msal-common/utils/ClientAssertionUtils.mjs +21 -0
  261. package/src/integrations/microsoft/assets/vendor/msal-common/utils/Constants.mjs +304 -0
  262. package/src/integrations/microsoft/assets/vendor/msal-common/utils/FunctionWrappers.mjs +95 -0
  263. package/src/integrations/microsoft/assets/vendor/msal-common/utils/ObjectUtils.mjs +27 -0
  264. package/src/integrations/microsoft/assets/vendor/msal-common/utils/ProtocolUtils.mjs +76 -0
  265. package/src/integrations/microsoft/assets/vendor/msal-common/utils/StringUtils.mjs +82 -0
  266. package/src/integrations/microsoft/assets/vendor/msal-common/utils/TimeUtils.mjs +75 -0
  267. package/src/integrations/microsoft/assets/vendor/msal-common/utils/UrlUtils.mjs +128 -0
@@ -0,0 +1,1780 @@
1
+ // Microsoft 365 (Graph/Entra) integration adapter. It owns only Microsoft
2
+ // OAuth/Graph HTTP; the provider-neutral engine owns persistence,
3
+ // reconciliation, and distillation.
4
+ //
5
+ // U13 implemented authorizationUrl/exchangeCode/refreshTokens. U14
6
+ // implements `discover` (Graph delta over the cursor contract). U15
7
+ // implements `fetch` (download + extractor routing + legacy pdf
8
+ // conversion). U16 (this unit) implements ensureWebhook/verifyWebhook
9
+ // (subscription fan-out per enrolled drive, stateless challenge echo,
10
+ // timing-safe clientState verification) and the optional
11
+ // answerWebhookChallenge/verifyLifecycleWebhook. U17/U18 land enrollment
12
+ // resolution/describeStatus.
13
+ import { randomBytes, randomUUID } from "node:crypto";
14
+ import { canRatify, canRead } from "../access/rbac.js";
15
+ import { DEFAULT_EXTRACT_LIMITS, extractText } from "../extract/index.js";
16
+ import { err, ok } from "../frontmatter/types.js";
17
+ import { DISTILL_NUMERIC_DEFAULTS } from "../utils/config.js";
18
+ import { SOURCE_STATUS_STATES } from "./engine.js";
19
+ import { boundedJson, DEFAULT_MAX_RESPONSE_BYTES, DEFAULT_REQUEST_TIMEOUT_MILLISECONDS, providerResponse, stringValue, TERMINAL_REFRESH_STATUSES, timingSafeSecretEqual, tokenExpiration, validHttpsUrl, } from "./http-json.js";
20
+ const MICROSOFT = "Microsoft";
21
+ // Microsoft identity platform v2.0 authorize/token endpoints.
22
+ const MICROSOFT_AUTHORITY_HOST = "https://login.microsoftonline.com";
23
+ const MICROSOFT_GRAPH_HOST = "https://graph.microsoft.com/v1.0";
24
+ // Server/Graph delegated scopes per scopeProfile (design: onedrive ->
25
+ // Files.Read, sharepoint -> Files.Read.All). The browser-MSAL SharePoint
26
+ // picker scopes (AllSites.Read/MyFiles.Read) are a different, client-side
27
+ // authorization surface and never belong on this server authorize URL.
28
+ const MICROSOFT_ONEDRIVE_SCOPE = "Files.Read";
29
+ const MICROSOFT_SHAREPOINT_SCOPE = "Files.Read.All";
30
+ // openid so the token response carries an id_token (used to read the tenant
31
+ // `tid` claim without a second round trip in the common case).
32
+ const MICROSOFT_BASE_SCOPES = "offline_access User.Read openid";
33
+ function requestUrl(path, parameters) {
34
+ const url = new URL(path);
35
+ for (const [key, value] of Object.entries(parameters)) {
36
+ if (value !== undefined)
37
+ url.searchParams.set(key, value);
38
+ }
39
+ return url.toString();
40
+ }
41
+ function tokenEndpoint(config) {
42
+ return `${MICROSOFT_AUTHORITY_HOST}/${config.tenantId}/oauth2/v2.0/token`;
43
+ }
44
+ function resolveScope(config) {
45
+ const resourceScope = config.scopeProfile === "onedrive" ? MICROSOFT_ONEDRIVE_SCOPE : MICROSOFT_SHAREPOINT_SCOPE;
46
+ return `${resourceScope} ${MICROSOFT_BASE_SCOPES}`;
47
+ }
48
+ // Bounded + timed-out request (design §11), returning the HTTP status
49
+ // alongside the parsed body regardless of 2xx/4xx/5xx — unlike the
50
+ // convenience jsonResponse in http-json.ts, callers here (refreshTokens
51
+ // especially) need the status/body of a non-2xx response to build a precise
52
+ // terminal signal. A transport failure or a timeout/size-cap trip returns a
53
+ // plain err() with no status attached, so it can never be misread as
54
+ // terminal (see TERMINAL_REFRESH_STATUSES usage in refreshTokens).
55
+ async function requestJson(transport, url, init, limits) {
56
+ const fetched = await providerResponse(MICROSOFT, transport, url, init, limits);
57
+ if (!fetched.ok)
58
+ return fetched;
59
+ const response = fetched.value;
60
+ const parsed = await boundedJson(MICROSOFT, response, limits);
61
+ if (!parsed.ok)
62
+ return parsed;
63
+ return ok({ status: response.status, body: parsed.value });
64
+ }
65
+ // Decodes (never verifies) the id_token's middle JWT segment to read the
66
+ // tenant `tid` claim. The value is only ever displayed/stored as metadata,
67
+ // never used as an authorization decision — signature verification is out of
68
+ // scope by design (see design §4.5).
69
+ function decodeIdTokenTenantId(idToken) {
70
+ const token = stringValue(idToken);
71
+ if (token === undefined)
72
+ return undefined;
73
+ const segments = token.split(".");
74
+ if (segments.length < 2)
75
+ return undefined;
76
+ try {
77
+ const payload = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
78
+ return stringValue(payload.tid);
79
+ }
80
+ catch {
81
+ return undefined;
82
+ }
83
+ }
84
+ async function resolveTenantId(transport, idToken, accessToken, limits) {
85
+ const fromIdToken = decodeIdTokenTenantId(idToken);
86
+ if (fromIdToken !== undefined)
87
+ return ok(fromIdToken);
88
+ const organization = await requestJson(transport, `${MICROSOFT_GRAPH_HOST}/organization`, { headers: { authorization: `Bearer ${accessToken}` } }, limits);
89
+ if (!organization.ok)
90
+ return organization;
91
+ if (organization.value.status < 200 || organization.value.status >= 300) {
92
+ return err(new Error(`Microsoft Graph organization lookup failed with status ${organization.value.status}`));
93
+ }
94
+ const body = organization.value.body;
95
+ const first = Array.isArray(body.value) ? body.value[0] : undefined;
96
+ const id = typeof first === "object" && first !== null
97
+ ? stringValue(first.id)
98
+ : undefined;
99
+ return id === undefined
100
+ ? err(new Error("Microsoft Graph organization response is missing a tenant id"))
101
+ : ok(id);
102
+ }
103
+ function terminalRefreshError(status, body) {
104
+ const errorCode = typeof body === "object" && body !== null
105
+ ? stringValue(body.error)
106
+ : undefined;
107
+ const error = new Error(`Microsoft OAuth token refresh failed with status ${status}${errorCode === undefined ? "" : `: ${errorCode}`}`);
108
+ error.status = status;
109
+ if (TERMINAL_REFRESH_STATUSES.has(status))
110
+ error.terminal = true;
111
+ return error;
112
+ }
113
+ function authorizationUrl(input, redirectUri, config) {
114
+ return requestUrl(`${MICROSOFT_AUTHORITY_HOST}/${config.tenantId}/oauth2/v2.0/authorize`, {
115
+ client_id: input.clientId,
116
+ code_challenge: input.codeChallenge,
117
+ code_challenge_method: input.codeChallengeMethod,
118
+ prompt: "select_account",
119
+ redirect_uri: redirectUri,
120
+ response_mode: "query",
121
+ response_type: "code",
122
+ scope: resolveScope(config),
123
+ state: input.state,
124
+ });
125
+ }
126
+ async function exchangeCode(transport, redirectUri, now, config, limits, input) {
127
+ const token = await requestJson(transport, tokenEndpoint(config), {
128
+ method: "POST",
129
+ headers: { "content-type": "application/x-www-form-urlencoded" },
130
+ body: new URLSearchParams({
131
+ client_id: input.clientId,
132
+ client_secret: input.clientSecret,
133
+ code: input.code,
134
+ code_verifier: input.pkceVerifier,
135
+ grant_type: "authorization_code",
136
+ redirect_uri: redirectUri,
137
+ scope: resolveScope(config),
138
+ }).toString(),
139
+ }, limits);
140
+ if (!token.ok)
141
+ return token;
142
+ if (token.value.status < 200 || token.value.status >= 300) {
143
+ return err(new Error(`Microsoft OAuth code exchange failed with status ${token.value.status}`));
144
+ }
145
+ const tokenBody = token.value.body;
146
+ const accessToken = stringValue(tokenBody.access_token);
147
+ const refreshToken = stringValue(tokenBody.refresh_token);
148
+ if (accessToken === undefined || refreshToken === undefined) {
149
+ return err(new Error("Microsoft OAuth token response is incomplete"));
150
+ }
151
+ const accessTokenExpiresAt = tokenExpiration(tokenBody.expires_in, now);
152
+ const tenantId = await resolveTenantId(transport, tokenBody.id_token, accessToken, limits);
153
+ if (!tenantId.ok)
154
+ return tenantId;
155
+ const me = await requestJson(transport, `${MICROSOFT_GRAPH_HOST}/me?$select=id,displayName,userPrincipalName`, { headers: { authorization: `Bearer ${accessToken}` } }, limits);
156
+ if (!me.ok)
157
+ return me;
158
+ if (me.value.status < 200 || me.value.status >= 300) {
159
+ return err(new Error(`Microsoft Graph /me lookup failed with status ${me.value.status}`));
160
+ }
161
+ const meBody = me.value.body;
162
+ const id = stringValue(meBody.id);
163
+ if (id === undefined)
164
+ return err(new Error("Microsoft Graph /me response is incomplete"));
165
+ const displayName = stringValue(meBody.displayName);
166
+ const upn = stringValue(meBody.userPrincipalName);
167
+ return ok({
168
+ accessToken,
169
+ refreshToken,
170
+ ...(accessTokenExpiresAt === undefined ? {} : { accessTokenExpiresAt }),
171
+ account: {
172
+ id,
173
+ tenantId: tenantId.value,
174
+ ...(displayName === undefined ? {} : { displayName }),
175
+ ...(upn === undefined ? {} : { upn }),
176
+ },
177
+ });
178
+ }
179
+ async function refreshTokens(transport, now, config, limits, input) {
180
+ const token = await requestJson(transport, tokenEndpoint(config), {
181
+ method: "POST",
182
+ headers: { "content-type": "application/x-www-form-urlencoded" },
183
+ body: new URLSearchParams({
184
+ client_id: input.clientId,
185
+ client_secret: input.clientSecret,
186
+ grant_type: "refresh_token",
187
+ refresh_token: input.refreshToken,
188
+ scope: resolveScope(config),
189
+ }).toString(),
190
+ }, limits);
191
+ if (!token.ok)
192
+ return token;
193
+ if (token.value.status < 200 || token.value.status >= 300) {
194
+ return err(terminalRefreshError(token.value.status, token.value.body));
195
+ }
196
+ const tokenBody = token.value.body;
197
+ const accessToken = stringValue(tokenBody.access_token);
198
+ if (accessToken === undefined) {
199
+ return err(new Error("Microsoft OAuth token refresh response is incomplete"));
200
+ }
201
+ // Microsoft rotates refresh tokens on every use; fall back to the prior
202
+ // refresh token only if the response omits a new one.
203
+ const refreshToken = stringValue(tokenBody.refresh_token) ?? input.refreshToken;
204
+ const accessTokenExpiresAt = tokenExpiration(tokenBody.expires_in, now);
205
+ return ok({
206
+ accessToken,
207
+ refreshToken,
208
+ ...(accessTokenExpiresAt === undefined ? {} : { accessTokenExpiresAt }),
209
+ });
210
+ }
211
+ // ---------------------------------------------------------------------------
212
+ // Webhooks (U16): one Graph subscription per enrolled drive, fanned out
213
+ // under a single stable channel id/secret; a stateless validation-token
214
+ // echo; timing-safe clientState verification; lifecycle -> queued action
215
+ // mapping. See design §6 (subscription model, validation, verification,
216
+ // lifecycle) and §11 (subscription limits).
217
+ // Graph's practical cap on a driveItem-resource subscription's lifetime is
218
+ // ~42300 minutes; 41000 minutes (design §6) leaves headroom under that cap
219
+ // while still comfortably clearing the engine's 24h renewal lead.
220
+ const MICROSOFT_SUBSCRIPTION_EXPIRATION_MINUTES = 41_000;
221
+ function subscriptionExpiration(now) {
222
+ return new Date(now.getTime() + MICROSOFT_SUBSCRIPTION_EXPIRATION_MINUTES * 60_000).toISOString();
223
+ }
224
+ function subscriptionsUrl() {
225
+ return `${MICROSOFT_GRAPH_HOST}/subscriptions`;
226
+ }
227
+ function subscriptionUrl(id) {
228
+ return `${MICROSOFT_GRAPH_HOST}/subscriptions/${encodeURIComponent(id)}`;
229
+ }
230
+ // Subscriptions are matched back to the drive they cover by parsing their
231
+ // own `resource` field (`/drives/{driveId}/root`) rather than threading a
232
+ // separate driveId alongside each stored subscription — the resource string
233
+ // is already the authoritative record of what a subscription covers.
234
+ function driveIdFromResource(resource) {
235
+ const match = /^\/drives\/([^/]+)\/root$/.exec(resource);
236
+ return match?.[1];
237
+ }
238
+ function subscriptionBody(driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime) {
239
+ return JSON.stringify({
240
+ changeType: "updated",
241
+ notificationUrl,
242
+ lifecycleNotificationUrl,
243
+ resource: `/drives/${driveId}/root`,
244
+ expirationDateTime,
245
+ clientState: secret,
246
+ });
247
+ }
248
+ async function createSubscription(transport, accessToken, limits, driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime) {
249
+ const response = await requestJson(transport, subscriptionsUrl(), {
250
+ method: "POST",
251
+ headers: { authorization: `Bearer ${accessToken}`, "content-type": "application/json" },
252
+ body: subscriptionBody(driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime),
253
+ }, limits);
254
+ if (!response.ok)
255
+ return response;
256
+ if (response.value.status < 200 || response.value.status >= 300) {
257
+ return err(new Error(`Microsoft Graph subscription create failed with status ${response.value.status}`));
258
+ }
259
+ const body = response.value.body;
260
+ const id = stringValue(body.id);
261
+ const expiresAt = stringValue(body.expirationDateTime);
262
+ if (id === undefined || expiresAt === undefined) {
263
+ return err(new Error("Microsoft Graph subscription create response is incomplete"));
264
+ }
265
+ return ok({ id, expiresAt });
266
+ }
267
+ // PATCH renews an existing subscription's expiration; a 404 means Graph has
268
+ // already forgotten it (expired past renewal, or externally deleted) so the
269
+ // only remaining option is to recreate it from scratch.
270
+ async function renewOrRecreateSubscription(transport, accessToken, limits, existingId, driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime) {
271
+ const patch = await requestJson(transport, subscriptionUrl(existingId), {
272
+ method: "PATCH",
273
+ headers: { authorization: `Bearer ${accessToken}`, "content-type": "application/json" },
274
+ body: JSON.stringify({ expirationDateTime }),
275
+ }, limits);
276
+ if (!patch.ok)
277
+ return patch;
278
+ if (patch.value.status === 404) {
279
+ return createSubscription(transport, accessToken, limits, driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime);
280
+ }
281
+ if (patch.value.status < 200 || patch.value.status >= 300) {
282
+ return err(new Error(`Microsoft Graph subscription renew failed with status ${patch.value.status}`));
283
+ }
284
+ const body = patch.value.body;
285
+ const id = stringValue(body.id) ?? existingId;
286
+ const expiresAt = stringValue(body.expirationDateTime) ?? expirationDateTime;
287
+ return ok({ id, expiresAt });
288
+ }
289
+ // A plain status check (not requestJson/boundedJson): Graph's DELETE
290
+ // response is 204 with no body, which a JSON-parsing helper would either
291
+ // reject or has to special-case — simpler to just confirm the status here.
292
+ async function deleteSubscription(transport, accessToken, limits, id) {
293
+ const response = await providerResponse(MICROSOFT, transport, subscriptionUrl(id), { method: "DELETE", headers: { authorization: `Bearer ${accessToken}` } }, limits);
294
+ if (!response.ok)
295
+ return response;
296
+ const status = response.value.status;
297
+ if (status !== 404 && (status < 200 || status >= 300)) {
298
+ return err(new Error(`Microsoft Graph subscription delete failed with status ${status}`));
299
+ }
300
+ return ok(undefined);
301
+ }
302
+ // Shared by ensureWebhook's channel-level expiresAt and describeStatus's
303
+ // webhook status (U18 follow-up): Date.parse, not a lexical string compare
304
+ // — Graph doesn't guarantee every expiresAt shares the same timezone
305
+ // designator or fractional-second precision, so ISO-8601 strings aren't
306
+ // safely comparable as plain strings.
307
+ function earliestSubscriptionExpiry(subscriptions) {
308
+ if (subscriptions.length === 0)
309
+ return undefined;
310
+ return subscriptions.reduce((min, current) => Date.parse(current.expiresAt) < Date.parse(min.expiresAt) ? current : min).expiresAt;
311
+ }
312
+ async function ensureWebhook(transport, state, input, limits) {
313
+ // Reused (not regenerated) across calls so a renewal keeps the same
314
+ // channel id and clientState secret, which every already-running
315
+ // subscription's clientState was signed with.
316
+ const id = state.webhook?.id ?? randomUUID();
317
+ const secret = state.webhook?.secret ?? randomBytes(32).toString("base64url");
318
+ // R19: no public HTTPS callback (e.g. loopback/local dev) -> fall back to
319
+ // polling. Never throw; never call Graph.
320
+ if (!validHttpsUrl(input.callbackUrl)) {
321
+ return ok({ id, secret, subscriptions: [] });
322
+ }
323
+ const driveIds = new Set((state.enrollment ?? []).map((record) => record.driveId));
324
+ const existingSubscriptions = state.webhook?.subscriptions ?? [];
325
+ const existingByDrive = new Map();
326
+ for (const subscription of existingSubscriptions) {
327
+ const driveId = driveIdFromResource(subscription.resource);
328
+ if (driveId !== undefined)
329
+ existingByDrive.set(driveId, subscription);
330
+ }
331
+ const notificationUrl = input.callbackUrl;
332
+ const lifecycleNotificationUrl = `${input.callbackUrl.replace(/\/$/, "")}/lifecycle`;
333
+ const expirationDateTime = subscriptionExpiration(input.now);
334
+ const accessToken = state.accessToken;
335
+ const subscriptions = [];
336
+ // Tracks every existing subscription id that this cycle keeps, renews, or
337
+ // recreates for a currently-enrolled drive — everything else still
338
+ // present in `existingSubscriptions` afterward (an unparseable/foreign
339
+ // resource, or a resource whose drive is no longer enrolled) is an
340
+ // orphan and gets deleted below, so a stale subscription never survives
341
+ // untracked against the §11 subscription limit.
342
+ const claimedSubscriptionIds = new Set();
343
+ for (const driveId of driveIds) {
344
+ const resource = `/drives/${driveId}/root`;
345
+ const current = existingByDrive.get(driveId);
346
+ if (current !== undefined)
347
+ claimedSubscriptionIds.add(current.id);
348
+ if (current !== undefined && Date.parse(current.expiresAt) > input.renewBefore.getTime()) {
349
+ subscriptions.push(current);
350
+ continue;
351
+ }
352
+ const result = current !== undefined
353
+ ? await renewOrRecreateSubscription(transport, accessToken, limits, current.id, driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime)
354
+ : await createSubscription(transport, accessToken, limits, driveId, secret, notificationUrl, lifecycleNotificationUrl, expirationDateTime);
355
+ if (!result.ok)
356
+ return result;
357
+ subscriptions.push({ id: result.value.id, resource, expiresAt: result.value.expiresAt });
358
+ }
359
+ // Every existing subscription not claimed above is orphaned — either its
360
+ // resource didn't parse to a driveId at all, or it parsed to a drive that
361
+ // no longer has a current enrollment — and is deleted unconditionally so
362
+ // it can never linger untracked on the Graph side.
363
+ for (const subscription of existingSubscriptions) {
364
+ if (claimedSubscriptionIds.has(subscription.id))
365
+ continue;
366
+ const deleted = await deleteSubscription(transport, accessToken, limits, subscription.id);
367
+ if (!deleted.ok)
368
+ return deleted;
369
+ }
370
+ const expiresAt = earliestSubscriptionExpiry(subscriptions);
371
+ return ok({
372
+ id,
373
+ secret,
374
+ subscriptions,
375
+ ...(expiresAt === undefined ? {} : { expiresAt }),
376
+ });
377
+ }
378
+ function answerWebhookChallenge(input) {
379
+ return input.query?.validationToken;
380
+ }
381
+ function parseWebhookNotificationBody(body) {
382
+ try {
383
+ return JSON.parse(Buffer.from(body).toString("utf8"));
384
+ }
385
+ catch {
386
+ return undefined;
387
+ }
388
+ }
389
+ async function verifyWebhook(input, state, now) {
390
+ const webhook = state.webhook;
391
+ if (webhook === undefined) {
392
+ return err(new Error("Microsoft webhook is not configured"));
393
+ }
394
+ const parsed = parseWebhookNotificationBody(input.body);
395
+ const body = parsed;
396
+ if (body === undefined || !Array.isArray(body.value) || body.value.length === 0) {
397
+ return err(new Error("Microsoft webhook notification body is invalid"));
398
+ }
399
+ const knownSubscriptionIds = new Set((webhook.subscriptions ?? []).map((s) => s.id));
400
+ // Drive-root notifications carry no item identity — every entry is
401
+ // validated (a batch is all-or-nothing), but the change itself only ever
402
+ // triggers a full reconcile, same as google.ts.
403
+ for (const raw of body.value) {
404
+ if (typeof raw !== "object" || raw === null) {
405
+ return err(new Error("Microsoft webhook notification entry is invalid"));
406
+ }
407
+ const entry = raw;
408
+ const subscriptionId = stringValue(entry.subscriptionId);
409
+ const clientState = stringValue(entry.clientState);
410
+ if (subscriptionId === undefined ||
411
+ !knownSubscriptionIds.has(subscriptionId) ||
412
+ clientState === undefined ||
413
+ !timingSafeSecretEqual(clientState, webhook.secret)) {
414
+ return err(new Error("Microsoft webhook notification is invalid"));
415
+ }
416
+ }
417
+ const first = body.value[0];
418
+ const subscriptionId = stringValue(first.subscriptionId);
419
+ // Graph retries a delivery under the same notification if it isn't
420
+ // acknowledged; when the payload carries no `id` of its own, minting a
421
+ // fresh random id (rather than a stable derivation from subscriptionId
422
+ // alone) means the queue's dedup-by-eventId does NOT catch a retried
423
+ // delivery — it gets re-enqueued and reprocessed as a distinct event.
424
+ // That's deliberate, not a missed optimization: this hint is always
425
+ // `{kind:"reconcile"}`, which is idempotent, so a harmless re-reconcile is
426
+ // the correct/safer trade against a stable id that could risk conflating
427
+ // two truly distinct notifications that happen to share a subscriptionId
428
+ // and arrive close together.
429
+ const eventId = stringValue(first.id) ?? `${subscriptionId}:${now().toISOString()}:${randomUUID()}`;
430
+ return ok({ kind: "event", eventId, hint: { kind: "reconcile" } });
431
+ }
432
+ function mapLifecycleAction(event) {
433
+ if (event === "reauthorizationRequired")
434
+ return "reauthorize";
435
+ if (event === "subscriptionRemoved")
436
+ return "recreate";
437
+ if (event === "missed")
438
+ return "reconcile";
439
+ return undefined;
440
+ }
441
+ async function verifyLifecycleWebhook(input, state, now) {
442
+ const webhook = state.webhook;
443
+ if (webhook === undefined) {
444
+ return err(new Error("Microsoft webhook is not configured"));
445
+ }
446
+ const parsed = parseWebhookNotificationBody(input.body);
447
+ const body = parsed;
448
+ if (body === undefined || !Array.isArray(body.value) || body.value.length === 0) {
449
+ return err(new Error("Microsoft lifecycle notification body is invalid"));
450
+ }
451
+ const first = body.value[0];
452
+ if (typeof first !== "object" || first === null) {
453
+ return err(new Error("Microsoft lifecycle notification entry is invalid"));
454
+ }
455
+ const entry = first;
456
+ const subscriptionId = stringValue(entry.subscriptionId);
457
+ const clientState = stringValue(entry.clientState);
458
+ const knownSubscriptionIds = new Set((webhook.subscriptions ?? []).map((s) => s.id));
459
+ if (subscriptionId === undefined ||
460
+ !knownSubscriptionIds.has(subscriptionId) ||
461
+ clientState === undefined ||
462
+ !timingSafeSecretEqual(clientState, webhook.secret)) {
463
+ return err(new Error("Microsoft lifecycle notification is invalid"));
464
+ }
465
+ const lifecycleEvent = stringValue(entry.lifecycleEvent);
466
+ const action = lifecycleEvent === undefined ? undefined : mapLifecycleAction(lifecycleEvent);
467
+ if (action === undefined) {
468
+ return err(new Error("Microsoft lifecycle notification has an unrecognized event"));
469
+ }
470
+ const eventId = stringValue(entry.id) ?? `${subscriptionId}:${now().toISOString()}:${randomUUID()}`;
471
+ return ok({ kind: "lifecycle", eventId, action });
472
+ }
473
+ // ---------------------------------------------------------------------------
474
+ // Discovery (U14): Microsoft Graph `delta` over the engine's opaque-cursor
475
+ // contract.
476
+ //
477
+ // Cursor threading mirrors google.ts EXACTLY: `discover` mutates
478
+ // `state.cursor` in place (only once every root has succeeded) and returns
479
+ // the discovered RemoteSource[] as its Result value. reconcileProvider
480
+ // (engine.ts ~636-673) snapshots `previousCursor` before calling discover,
481
+ // lets discover mutate `providerState.cursor` freely, then immediately rolls
482
+ // it back to `previousCursor` ("provisional") until every scoped source has
483
+ // been fetched/distilled without failure — only then does it re-apply the
484
+ // mutated value. If discover() itself returns err (e.g. one root's request
485
+ // was rate-limited past the retry budget), reconcileProvider returns before
486
+ // ever reading `providerState.cursor` again, and — because this whole
487
+ // invocation runs inside withIntegrationStateLock without a matching
488
+ // writeState — nothing is persisted. So this function's own contract is:
489
+ // mutate `state.cursor` only after EVERY root has fully succeeded; return
490
+ // err() untouched otherwise. That "all-or-nothing" cursor mutation is a
491
+ // belt-and-braces mirror of the engine's own commit-on-success rule, not a
492
+ // substitute for it.
493
+ //
494
+ // The cursor is `{"v":1,"roots":{"<cursorKey>":"<@odata.deltaLink>"}}` — an
495
+ // opaque string to the engine, parsed defensively (garbage/missing -> no
496
+ // roots, i.e. every root initializes fresh).
497
+ const MICROSOFT_DELTA_SELECT = "id,name,eTag,cTag,size,file,folder,deleted,parentReference,malware";
498
+ const MICROSOFT_DELTA_PREFER = "deltaExcludeParent";
499
+ const MICROSOFT_MAX_DELTA_PAGES = 1_000;
500
+ // Graph honors Retry-After on 429s; bounded to a single retry and only when
501
+ // the wait is short enough that a reconcile cycle can absorb it inline (an
502
+ // unbounded/looping retry would starve the cycle — see http-json.ts's "don't
503
+ // add unbounded retries" note, which this mirrors at the delta layer since
504
+ // the shared helper has no built-in 429 semantics).
505
+ const MICROSOFT_MAX_RETRY_AFTER_SECONDS = 30;
506
+ function parseMicrosoftCursor(raw) {
507
+ if (raw === undefined)
508
+ return {};
509
+ let parsed;
510
+ try {
511
+ parsed = JSON.parse(raw);
512
+ }
513
+ catch {
514
+ return {};
515
+ }
516
+ if (typeof parsed !== "object" || parsed === null)
517
+ return {};
518
+ const roots = parsed.roots;
519
+ if (typeof roots !== "object" || roots === null)
520
+ return {};
521
+ const result = {};
522
+ for (const [cursorKey, value] of Object.entries(roots)) {
523
+ if (typeof value === "string" && value.length > 0) {
524
+ result[cursorKey] = value;
525
+ continue;
526
+ }
527
+ if (typeof value !== "object" || value === null)
528
+ continue;
529
+ const link = stringValue(value.link);
530
+ if (link === undefined)
531
+ continue;
532
+ const rawFolders = value.folders;
533
+ const folders = Array.isArray(rawFolders)
534
+ ? rawFolders.filter((id) => typeof id === "string" && id.length > 0)
535
+ : [];
536
+ result[cursorKey] = { link, folders };
537
+ }
538
+ return result;
539
+ }
540
+ function serializeMicrosoftCursor(roots) {
541
+ return JSON.stringify({ v: 1, roots });
542
+ }
543
+ function cursorEntryLink(entry) {
544
+ if (entry === undefined)
545
+ return undefined;
546
+ return typeof entry === "string" ? entry : entry.link;
547
+ }
548
+ function cursorEntryFolders(entry) {
549
+ if (entry === undefined || typeof entry === "string")
550
+ return [];
551
+ return entry.folders ?? [];
552
+ }
553
+ function deriveMicrosoftDeltaRoots(enrollments) {
554
+ const roots = new Map();
555
+ for (const record of enrollments ?? []) {
556
+ if (record.kind === "folder") {
557
+ roots.set(record.cursorKey, {
558
+ cursorKey: record.cursorKey,
559
+ kind: "container",
560
+ driveId: record.driveId,
561
+ folderId: record.remoteId,
562
+ });
563
+ continue;
564
+ }
565
+ const existing = roots.get(record.cursorKey);
566
+ if (existing !== undefined && existing.kind === "item" && existing.memberIds !== undefined) {
567
+ existing.memberIds.add(record.remoteId);
568
+ continue;
569
+ }
570
+ roots.set(record.cursorKey, {
571
+ cursorKey: record.cursorKey,
572
+ kind: "item",
573
+ driveId: record.driveId,
574
+ memberIds: new Set([record.remoteId]),
575
+ });
576
+ }
577
+ return [...roots.values()];
578
+ }
579
+ // The per-root "remembered" set that incremental delta pages get merged onto
580
+ // (Graph delta returns only CHANGED items after the first page, so anything
581
+ // untouched since the last cursor must carry forward from prior state).
582
+ // ProviderState.sources has no per-root tag, so this reconstructs root
583
+ // membership from the id namespace itself (`<driveId>:<itemId>`) plus, for
584
+ // item-group roots, the current enrollment's member ids. KNOWN LIMITATION
585
+ // (flagged for self-review, bead filed as a follow-up): if two roots share
586
+ // the same driveId (e.g. two separate container enrollments inside one
587
+ // SharePoint drive), this prefix filter can't disambiguate which root a
588
+ // remembered item belongs to. This is NOT merely redundant work — it is an
589
+ // availability-state correctness bug: an item DELETED from root A's subtree
590
+ // (and correctly dropped by root A's own walk) can be silently RESURRECTED
591
+ // into the merged present set because root B's remembered-set filter still
592
+ // matches it by driveId prefix and re-seeds it, unchanged, into root B's
593
+ // walk. That defeats R37's not-returned -> unavailable contract for that
594
+ // item. The fix is not "widen EnrollmentRecord" — EnrollmentRecord already
595
+ // carries driveId/cursorKey/id, i.e. everything needed to know which root an
596
+ // item truly belongs to. The missing wiring is downstream, in the
597
+ // provider-neutral engine: RemoteSource/NormalizedRemoteSource carry only
598
+ // {id, revision} with no root/cursorKey tag, and engine.ts's sourceState()
599
+ // never populates SourceState.enrollmentId for any provider, so
600
+ // state.sources has nowhere to record which root produced an entry. Fixing
601
+ // this properly means threading a root/enrollment tag through the engine's
602
+ // RemoteSource contract for every adapter, not just Microsoft — out of this
603
+ // unit's scope; a follow-up bead tracks it. No required test scenario for
604
+ // U14 exercises this cross-root case.
605
+ function rememberedRootSources(state, root) {
606
+ const prefix = `${root.driveId}:`;
607
+ const sources = new Map();
608
+ for (const source of Object.values(state.sources)) {
609
+ if (!source.available || !source.id.startsWith(prefix))
610
+ continue;
611
+ if (typeof source.revision !== "string" || source.revision.length === 0)
612
+ continue;
613
+ if (root.kind === "item") {
614
+ const itemIdPart = source.id.slice(prefix.length);
615
+ if (!root.memberIds?.has(itemIdPart))
616
+ continue;
617
+ }
618
+ sources.set(source.id, { id: source.id, revision: source.revision });
619
+ }
620
+ return sources;
621
+ }
622
+ function microsoftDeltaSelectParameters(extra = {}) {
623
+ return { $select: MICROSOFT_DELTA_SELECT, ...extra };
624
+ }
625
+ function containerInitUrl(driveId, folderId) {
626
+ return requestUrl(`${MICROSOFT_GRAPH_HOST}/drives/${encodeURIComponent(driveId)}/items/${encodeURIComponent(folderId)}/delta`, microsoftDeltaSelectParameters());
627
+ }
628
+ // Probe-deferred fallback (design note): a folder-scoped delta 400s on some
629
+ // SharePoint libraries; probe 2 will confirm whether the folder-scoped or
630
+ // drive-root path is primary in practice. Until then, this is reached only
631
+ // when a fresh (non-resumed) container init 400s, or when a root already
632
+ // known (via its persisted cursor entry shape) to be in fallback mode
633
+ // resyncs.
634
+ function containerFallbackUrl(driveId) {
635
+ return requestUrl(`${MICROSOFT_GRAPH_HOST}/drives/${encodeURIComponent(driveId)}/root/delta`, microsoftDeltaSelectParameters());
636
+ }
637
+ function itemGroupInitUrl(driveId) {
638
+ return requestUrl(`${MICROSOFT_GRAPH_HOST}/drives/${encodeURIComponent(driveId)}/root/delta`, microsoftDeltaSelectParameters({ token: "latest" }));
639
+ }
640
+ function deltaItemId(raw) {
641
+ if (typeof raw !== "object" || raw === null)
642
+ return undefined;
643
+ return stringValue(raw.id);
644
+ }
645
+ function deltaParentId(item) {
646
+ if (typeof item.parentReference !== "object" || item.parentReference === null)
647
+ return undefined;
648
+ return stringValue(item.parentReference.id);
649
+ }
650
+ // Item-group roots only ever track the specifically-enrolled item ids — the
651
+ // drive-root delta walks the WHOLE drive, so everything else must be
652
+ // filtered out (§8: a large library is never fully enumerated into sources).
653
+ // No ancestry/parent reasoning applies here (membership is a flat id
654
+ // lookup), so this is unaffected by the U14 Critical-bug fix below.
655
+ function classifyItemGroupDeltaItem(raw, driveId, memberIds) {
656
+ const id = deltaItemId(raw);
657
+ if (id === undefined || !memberIds.has(id))
658
+ return undefined;
659
+ const item = raw;
660
+ const sourceId = `${driveId}:${id}`;
661
+ if (item.deleted !== undefined || item.file === undefined) {
662
+ return { id: sourceId, action: "remove" };
663
+ }
664
+ const eTag = stringValue(item.eTag);
665
+ if (eTag === undefined)
666
+ return { id: sourceId, action: "remove" };
667
+ return { id: sourceId, action: "upsert", revision: eTag };
668
+ }
669
+ // PRIMARY container path (folder-scoped delta, `/drives/{d}/items/{f}/delta`):
670
+ // Graph itself scopes every returned item to that folder's subtree, so NO
671
+ // parent/ancestry filtering is applied here — trust Graph's own scoping.
672
+ //
673
+ // PROBE-2-DEPENDENT ASSUMPTION: "folder-scoped delta is subtree-scoped" is
674
+ // currently unverified against a real tenant — the same open question as
675
+ // whether/when it 400s (see containerFallbackUrl's "probe-deferred" note
676
+ // below; both are the same probe). If it turns out folder-scoped delta on
677
+ // some SharePoint library is NOT subtree-scoped, this path would UNDER-filter
678
+ // (include out-of-subtree items instead of excluding them) — a real residual
679
+ // risk, but a strictly lower-severity failure mode than the Critical bug this
680
+ // unit fixed: an under-filter means a few extra items get distilled that
681
+ // shouldn't be, not a legitimately-enrolled item silently disappearing. Track
682
+ // and confirm/correct when probe 2 runs against a live tenant.
683
+ //
684
+ // U14 Critical-bug fix note: an earlier version of this adapter re-derived
685
+ // ancestry from scratch every discover() call (`new Set([folderId])`) and
686
+ // applied an "outside subtree -> remove" check even on this primary path.
687
+ // That is provably wrong on a RESUMED incremental cycle: Graph delta sends
688
+ // only CHANGED items, so an edited file two-or-more levels under the
689
+ // enrolled folder can arrive in a page where its (unchanged) parent
690
+ // subfolder record is absent. With only `{folderId}` seeded, the file's
691
+ // `parentReference.id` never resolves into the ancestor set, so it was
692
+ // wrongly classified "outside subtree" and silently dropped — a real, silent
693
+ // data-loss bug on ordinary nested-folder edits. Leaning on Graph's own
694
+ // folder-scoped delta guarantee for this path removes the need to
695
+ // reconstruct ancestry at all. See classifyContainerFallback below for the
696
+ // one path (drive-root fallback) where ancestry genuinely has to be tracked,
697
+ // and how it's made safe across cycles.
698
+ function classifyContainerPrimary(raw, driveId, folderId) {
699
+ const id = deltaItemId(raw);
700
+ if (id === undefined || id === folderId)
701
+ return undefined; // root folder itself is not a source
702
+ const item = raw;
703
+ const sourceId = `${driveId}:${id}`;
704
+ if (item.deleted !== undefined)
705
+ return { id: sourceId, action: "remove" };
706
+ if (item.folder !== undefined)
707
+ return undefined; // folders are never sources
708
+ const eTag = stringValue(item.eTag);
709
+ if (item.file === undefined || eTag === undefined)
710
+ return { id: sourceId, action: "remove" };
711
+ return { id: sourceId, action: "upsert", revision: eTag };
712
+ }
713
+ // FALLBACK container path only (drive-root delta, used when the primary
714
+ // folder-scoped delta 400s): the drive-root delta returns the WHOLE drive,
715
+ // so ancestry filtering by parentReference.id IS genuinely required here to
716
+ // restrict results to the enrolled subtree (delta omits parentReference.path
717
+ // entirely). Made safe against both bugs the Critical-bug review flagged:
718
+ //
719
+ // 1. Cross-cycle: the folder-id set discovered so far is PERSISTED in the
720
+ // cursor (see MicrosoftCursorRootEntry.folders) and seeded back in on
721
+ // every resumed walk, so an unchanged ancestor folder not resent this
722
+ // cycle doesn't erase what was already learned.
723
+ // 2. Intra-walk ordering: `foldContainerAncestry` runs a two-pass fixpoint
724
+ // over ALL items collected across every page of this walk BEFORE any
725
+ // file is classified, so a nested file arriving before its subfolder
726
+ // record (same page or a later one) still resolves correctly.
727
+ //
728
+ // Conservative-on-ambiguity rule: a file whose parent can't be resolved into
729
+ // the (fully folded) ancestor set is never REMOVED if it was already
730
+ // tracked — only an explicit `deleted` facet removes a previously-known
731
+ // item. An unresolved parent only prevents ADDING a not-yet-tracked item
732
+ // (the drive-root delta covers unrelated parts of the drive too, so an
733
+ // unresolved new item is presumed foreign, not ours). This trades a
734
+ // possible late remove (an item that truly moved out lingers until an
735
+ // explicit signal or a full resync) for never silently losing a legitimately
736
+ // in-scope item — the correct tradeoff per the Critical-bug review.
737
+ //
738
+ // KNOWN, DELIBERATE DEVIATION from R21/R37 (fallback path ONLY): R21 calls
739
+ // for removing an item whose parent has moved out of the enrolled subtree;
740
+ // R37 calls for that to resolve to available:false. In THIS path only, a
741
+ // genuinely moved-out item does NOT do either — it lingers in the present
742
+ // set (available:true, stale revision) until either an explicit `deleted`
743
+ // facet arrives or the root's next full resync (410) re-derives membership
744
+ // from scratch. That is a conscious trade, not an oversight: the
745
+ // alternative (treat an unresolved/ambiguous parent as "moved out") is
746
+ // exactly the mechanism that caused the Critical silent-data-loss bug this
747
+ // unit fixed, just relocated from every nested file to the narrower
748
+ // moved-out case. The primary folder-scoped path is NOT affected by this
749
+ // deviation — there, a moved-out file simply stops being returned by
750
+ // Graph's own subtree-scoped delta, so the engine's ordinary
751
+ // not-returned -> unavailable rule (R37) still fires correctly. A filed
752
+ // bead tracks revisiting this fallback-path gap once probe 2 confirms
753
+ // folder-scoped delta's real-world 400 rate; if folder-scoped delta turns
754
+ // out to be reliable enough, the fallback path (and this deviation) may
755
+ // rarely if ever be exercised in practice.
756
+ function applyContainerFallbackItem(raw, driveId, folderId, ancestorIds, sources) {
757
+ const id = deltaItemId(raw);
758
+ if (id === undefined || id === folderId)
759
+ return; // root folder itself is not a source
760
+ const item = raw;
761
+ const sourceId = `${driveId}:${id}`;
762
+ if (item.deleted !== undefined) {
763
+ sources.delete(sourceId);
764
+ return;
765
+ }
766
+ if (item.folder !== undefined)
767
+ return; // folders: ancestry only, handled by foldContainerAncestry
768
+ const parentId = deltaParentId(item);
769
+ const inSubtree = parentId !== undefined && ancestorIds.has(parentId);
770
+ const eTag = stringValue(item.eTag);
771
+ if (inSubtree && item.file !== undefined && eTag !== undefined) {
772
+ sources.set(sourceId, { id: sourceId, revision: eTag });
773
+ return;
774
+ }
775
+ if (inSubtree) {
776
+ // Confirmed in-subtree but not a usable file record (missing eTag, or a
777
+ // non-file facet replacing a former file) — a definite signal, safe to
778
+ // drop.
779
+ sources.delete(sourceId);
780
+ return;
781
+ }
782
+ // Parent unresolved (or resolved outside the known ancestor set): never
783
+ // speculatively add an unconfirmed item, but never drop a previously-known
784
+ // one either. See the conservative-on-ambiguity note above.
785
+ }
786
+ // Two-pass ancestry fold (part of the drive-root fallback path only): grows
787
+ // `ancestorIds` from `{folderId} ∪ persistedFolders` by repeatedly scanning
788
+ // every folder record collected across the WHOLE walk (all pages) until no
789
+ // further growth occurs. Running this to a fixpoint over the complete item
790
+ // set — rather than once, streaming, per page — is what makes a nested
791
+ // file's classification independent of whether its subfolder's record
792
+ // happens to arrive before or after it.
793
+ function foldContainerAncestry(items, folderId, persistedFolders) {
794
+ const ancestorIds = new Set([folderId, ...persistedFolders]);
795
+ let grew = true;
796
+ while (grew) {
797
+ grew = false;
798
+ for (const raw of items) {
799
+ if (typeof raw !== "object" || raw === null)
800
+ continue;
801
+ const item = raw;
802
+ if (item.folder === undefined || item.deleted !== undefined)
803
+ continue;
804
+ const id = stringValue(item.id);
805
+ if (id === undefined || ancestorIds.has(id))
806
+ continue;
807
+ const parentId = deltaParentId(item);
808
+ if (parentId !== undefined && ancestorIds.has(parentId)) {
809
+ ancestorIds.add(id);
810
+ grew = true;
811
+ }
812
+ }
813
+ }
814
+ return ancestorIds;
815
+ }
816
+ function taggedDeltaError(message, tags) {
817
+ return Object.assign(new Error(message), tags);
818
+ }
819
+ function isResyncError(error) {
820
+ return error.resync === true;
821
+ }
822
+ function isBadRequestError(error) {
823
+ return error.badRequest === true;
824
+ }
825
+ async function requestDeltaPage(transport, url, accessToken, limits) {
826
+ const fetched = await providerResponse(MICROSOFT, transport, url, { headers: { authorization: `Bearer ${accessToken}`, prefer: MICROSOFT_DELTA_PREFER } }, limits);
827
+ if (!fetched.ok)
828
+ return fetched;
829
+ const response = fetched.value;
830
+ const retryAfterHeader = response.headers.get("retry-after");
831
+ const retryAfterSeconds = retryAfterHeader !== null && /^\d+$/.test(retryAfterHeader)
832
+ ? Number(retryAfterHeader)
833
+ : undefined;
834
+ if (response.status !== 200) {
835
+ return ok({
836
+ status: response.status,
837
+ ...(retryAfterSeconds === undefined ? {} : { retryAfterSeconds }),
838
+ });
839
+ }
840
+ const parsed = await boundedJson(MICROSOFT, response, limits);
841
+ if (!parsed.ok)
842
+ return parsed;
843
+ return ok({ status: response.status, body: parsed.value });
844
+ }
845
+ // Honors Retry-After exactly once, only when short enough to absorb inline;
846
+ // a second 429 (or a longer wait) is a hard failure so this can never retry
847
+ // unboundedly.
848
+ async function requestDeltaPageWithRetry(transport, url, accessToken, limits, sleep) {
849
+ const first = await requestDeltaPage(transport, url, accessToken, limits);
850
+ if (!first.ok)
851
+ return first;
852
+ if (first.value.status !== 429)
853
+ return first;
854
+ const retryAfterSeconds = first.value.retryAfterSeconds;
855
+ if (retryAfterSeconds === undefined || retryAfterSeconds > MICROSOFT_MAX_RETRY_AFTER_SECONDS) {
856
+ return err(new Error("Microsoft Graph delta request was rate limited"));
857
+ }
858
+ await sleep(retryAfterSeconds * 1000);
859
+ const retried = await requestDeltaPage(transport, url, accessToken, limits);
860
+ if (!retried.ok)
861
+ return retried;
862
+ if (retried.value.status === 429) {
863
+ return err(new Error("Microsoft Graph delta request was rate limited"));
864
+ }
865
+ return retried;
866
+ }
867
+ // Follows @odata.nextLink pages from `initialUrl` to a terminal
868
+ // @odata.deltaLink, bounded by MICROSOFT_MAX_DELTA_PAGES with a repeated-link
869
+ // guard (mirrors google.ts's Drive-changes pagination exactly). Deliberately
870
+ // does NOT classify items itself — it just collects them in stream order —
871
+ // so a caller needing ancestry (the container fallback path) can fold
872
+ // ancestry over the COMPLETE item set before classifying anything, and a
873
+ // caller that doesn't (item-group roots, container primary) can classify in
874
+ // one pass. Last-occurrence-wins and delete-reappears-as-present both fall
875
+ // out naturally from classifying this list in its original (page) order into
876
+ // a Map.
877
+ async function walkMicrosoftDeltaPages(transport, accessToken, limits, sleep, initialUrl) {
878
+ const items = [];
879
+ const seenLinks = new Set();
880
+ let url = initialUrl;
881
+ let pages = 0;
882
+ for (;;) {
883
+ pages += 1;
884
+ if (pages > MICROSOFT_MAX_DELTA_PAGES) {
885
+ return err(new Error("Microsoft Graph delta discovery exceeds the page limit"));
886
+ }
887
+ if (seenLinks.has(url)) {
888
+ return err(new Error("Microsoft Graph delta discovery repeated a page link"));
889
+ }
890
+ seenLinks.add(url);
891
+ const page = await requestDeltaPageWithRetry(transport, url, accessToken, limits, sleep);
892
+ if (!page.ok)
893
+ return page;
894
+ if (page.value.status === 410) {
895
+ return err(taggedDeltaError("Microsoft Graph delta requires a resync", { resync: true }));
896
+ }
897
+ if (page.value.status === 400) {
898
+ return err(taggedDeltaError(`Microsoft Graph delta request failed with status 400`, {
899
+ badRequest: true,
900
+ }));
901
+ }
902
+ if (page.value.status !== 200) {
903
+ return err(new Error(`Microsoft Graph delta request failed with status ${page.value.status}`));
904
+ }
905
+ const body = page.value.body;
906
+ if (body.value !== undefined && !Array.isArray(body.value)) {
907
+ return err(new Error("Microsoft Graph delta response is invalid"));
908
+ }
909
+ for (const raw of body.value ?? [])
910
+ items.push(raw);
911
+ const deltaLink = stringValue(body["@odata.deltaLink"]);
912
+ if (deltaLink !== undefined)
913
+ return ok({ deltaLink, items });
914
+ const nextLink = stringValue(body["@odata.nextLink"]);
915
+ if (nextLink === undefined) {
916
+ return err(new Error("Microsoft Graph delta response is missing a nextLink or deltaLink"));
917
+ }
918
+ url = nextLink;
919
+ }
920
+ }
921
+ async function walkContainerRoot(transport, accessToken, limits, sleep, root, storedEntry, remembered) {
922
+ const folderId = root.folderId;
923
+ const runPrimary = async (url, seed) => {
924
+ const pages = await walkMicrosoftDeltaPages(transport, accessToken, limits, sleep, url);
925
+ if (!pages.ok)
926
+ return pages;
927
+ const sources = new Map(seed);
928
+ for (const raw of pages.value.items) {
929
+ const classified = classifyContainerPrimary(raw, root.driveId, folderId);
930
+ if (classified === undefined)
931
+ continue;
932
+ if (classified.action === "remove")
933
+ sources.delete(classified.id);
934
+ else
935
+ sources.set(classified.id, { id: classified.id, revision: classified.revision });
936
+ }
937
+ return ok({ mode: "primary", deltaLink: pages.value.deltaLink, sources });
938
+ };
939
+ const runFallback = async (url, seed, persistedFolders) => {
940
+ const pages = await walkMicrosoftDeltaPages(transport, accessToken, limits, sleep, url);
941
+ if (!pages.ok)
942
+ return pages;
943
+ const ancestorIds = foldContainerAncestry(pages.value.items, folderId, persistedFolders);
944
+ const sources = new Map(seed);
945
+ for (const raw of pages.value.items) {
946
+ applyContainerFallbackItem(raw, root.driveId, folderId, ancestorIds, sources);
947
+ }
948
+ return ok({
949
+ mode: "fallback",
950
+ deltaLink: pages.value.deltaLink,
951
+ folders: [...ancestorIds],
952
+ sources,
953
+ });
954
+ };
955
+ // A fresh (non-resumed) walk always tries the primary folder-scoped delta
956
+ // first, falling back to the drive-root delta only if that 400s.
957
+ const freshWalk = () => runPrimary(containerInitUrl(root.driveId, folderId), new Map()).then((primary) => !primary.ok && isBadRequestError(primary.error)
958
+ ? runFallback(containerFallbackUrl(root.driveId), new Map(), [folderId])
959
+ : primary);
960
+ const resumedLink = cursorEntryLink(storedEntry);
961
+ // The persisted cursor entry's own shape says which mode a resumed walk is
962
+ // in — a bare string means primary (no ancestry needed), an object with a
963
+ // `folders` array means fallback (see MicrosoftCursorRootEntry above) —
964
+ // so a resumed walk never needs to re-probe with a 400 to rediscover this.
965
+ const resumedMode = resumedLink === undefined
966
+ ? undefined
967
+ : typeof storedEntry === "string"
968
+ ? "primary"
969
+ : "fallback";
970
+ let result;
971
+ if (resumedLink === undefined) {
972
+ result = await freshWalk();
973
+ }
974
+ else if (resumedMode === "fallback") {
975
+ result = await runFallback(resumedLink, remembered, cursorEntryFolders(storedEntry));
976
+ }
977
+ else {
978
+ result = await runPrimary(resumedLink, remembered);
979
+ }
980
+ // 410 resync: drop this root's stored link/folders and re-enumerate from
981
+ // scratch, bounded to a single re-init attempt so a persistently-invalid
982
+ // delta session can't loop forever. A root already known to be in
983
+ // fallback mode re-inits straight into the fallback endpoint (it already
984
+ // positively knows the primary endpoint 400s for this drive); everything
985
+ // else re-runs the fresh-walk probe.
986
+ if (!result.ok && isResyncError(result.error)) {
987
+ result =
988
+ resumedMode === "fallback"
989
+ ? await runFallback(containerFallbackUrl(root.driveId), new Map(), [folderId])
990
+ : await freshWalk();
991
+ }
992
+ return result;
993
+ }
994
+ async function walkItemGroupRoot(transport, accessToken, limits, sleep, root, storedLink, remembered) {
995
+ const memberIds = root.memberIds;
996
+ const run = async (url, seed) => {
997
+ const pages = await walkMicrosoftDeltaPages(transport, accessToken, limits, sleep, url);
998
+ if (!pages.ok)
999
+ return pages;
1000
+ const sources = new Map(seed);
1001
+ for (const raw of pages.value.items) {
1002
+ const classified = classifyItemGroupDeltaItem(raw, root.driveId, memberIds);
1003
+ if (classified === undefined)
1004
+ continue;
1005
+ if (classified.action === "remove")
1006
+ sources.delete(classified.id);
1007
+ else
1008
+ sources.set(classified.id, { id: classified.id, revision: classified.revision });
1009
+ }
1010
+ return ok({ deltaLink: pages.value.deltaLink, sources });
1011
+ };
1012
+ let result = storedLink !== undefined
1013
+ ? await run(storedLink, remembered)
1014
+ : await run(itemGroupInitUrl(root.driveId), new Map());
1015
+ // 410 resync: re-enumerate this root only, bounded to a single re-init.
1016
+ if (!result.ok && isResyncError(result.error)) {
1017
+ result = await run(itemGroupInitUrl(root.driveId), new Map());
1018
+ }
1019
+ return result;
1020
+ }
1021
+ async function discoverMicrosoftSources(transport, limits, sleep, state) {
1022
+ const roots = deriveMicrosoftDeltaRoots(state.enrollment);
1023
+ if (roots.length === 0)
1024
+ return ok([]);
1025
+ const storedRoots = parseMicrosoftCursor(state.cursor);
1026
+ const newRoots = {};
1027
+ const allSources = new Map();
1028
+ for (const root of roots) {
1029
+ const remembered = rememberedRootSources(state, root);
1030
+ if (root.kind === "container") {
1031
+ const walked = await walkContainerRoot(transport, state.accessToken, limits, sleep, root, storedRoots[root.cursorKey], remembered);
1032
+ if (!walked.ok)
1033
+ return walked;
1034
+ newRoots[root.cursorKey] =
1035
+ walked.value.mode === "fallback"
1036
+ ? { link: walked.value.deltaLink, folders: walked.value.folders ?? [] }
1037
+ : walked.value.deltaLink;
1038
+ // #506: tag every descendant with the owning folder enrollment's ref
1039
+ // (`${driveId}:${folderId}`, byte-identical to that enrollment's `ref`)
1040
+ // so the engine's owningEnrollment() attaches its targetCollection.
1041
+ const enrolledRef = `${root.driveId}:${root.folderId}`;
1042
+ for (const [id, source] of walked.value.sources) {
1043
+ allSources.set(id, { ...source, enrolledRef });
1044
+ }
1045
+ }
1046
+ else {
1047
+ const walked = await walkItemGroupRoot(transport, state.accessToken, limits, sleep, root, cursorEntryLink(storedRoots[root.cursorKey]), remembered);
1048
+ if (!walked.ok)
1049
+ return walked;
1050
+ newRoots[root.cursorKey] = walked.value.deltaLink;
1051
+ // An item enrollment's ref IS its source id, so enrolledRef defaults to
1052
+ // id in owningEnrollment(); set it explicitly for symmetry/robustness.
1053
+ for (const [id, source] of walked.value.sources) {
1054
+ allSources.set(id, { ...source, enrolledRef: id });
1055
+ }
1056
+ }
1057
+ }
1058
+ // Mutate state.cursor only now that every root has fully succeeded — same
1059
+ // "all roots or none" cursor mutation the engine itself enforces on top of
1060
+ // this (see the big comment above this section).
1061
+ state.cursor = serializeMicrosoftCursor(newRoots);
1062
+ return ok([...allSources.values()]);
1063
+ }
1064
+ function defaultSleep(milliseconds) {
1065
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
1066
+ }
1067
+ // ---------------------------------------------------------------------------
1068
+ // Fetch (U15): download + extractor routing + legacy pdf conversion.
1069
+ //
1070
+ // `source.id` is always `"<driveId>:<itemId>"` (the identity discover()
1071
+ // produces — see discoverMicrosoftSources above). This section owns turning
1072
+ // that id back into a Graph download, routing the downloaded bytes to
1073
+ // src/extract by file extension, and returning the provider-neutral
1074
+ // {id, revision, text} triple the engine persists.
1075
+ // 25 MiB (design §11) — the SAME cap is checked twice: against the metadata
1076
+ // `size` field BEFORE any download is attempted (the primary guard), and
1077
+ // again while streaming the content response (belt-and-braces against a
1078
+ // metadata/body size mismatch).
1079
+ const MICROSOFT_MAX_CONTENT_BYTES = 25 * 1024 * 1024;
1080
+ // R30: a prior failure with one of these reasons is a durable, re-download-
1081
+ // proof verdict on THIS revision of the file — malware/size/type are
1082
+ // properties of the bytes/metadata that a byte-identical re-download cannot
1083
+ // change. A prior "fetch"/"distill"/"timeout"/etc. failure is NOT in this
1084
+ // set: those can be transient (a network blip, a slow extractor that might
1085
+ // succeed on retry), so those always re-attempt.
1086
+ const MICROSOFT_SHORT_CIRCUIT_REASONS = new Set([
1087
+ "empty",
1088
+ "encrypted",
1089
+ "unsupported_type",
1090
+ "too_large",
1091
+ ]);
1092
+ function taggedFetchError(reason, message) {
1093
+ return Object.assign(new Error(message ?? `Microsoft fetch failed: ${reason}`), { reason });
1094
+ }
1095
+ function splitSourceId(id) {
1096
+ const index = id.indexOf(":");
1097
+ if (index <= 0 || index === id.length - 1)
1098
+ return undefined;
1099
+ return { driveId: id.slice(0, index), itemId: id.slice(index + 1) };
1100
+ }
1101
+ function fileExtension(name) {
1102
+ const index = name.lastIndexOf(".");
1103
+ return index === -1 ? "" : name.slice(index).toLowerCase();
1104
+ }
1105
+ // §3.1 extension->path table. `.doc`/`.ppt` route through the legacy
1106
+ // Graph `?format=pdf` conversion (R28) instead of a native extractor.
1107
+ function classifyExtension(name) {
1108
+ const extension = fileExtension(name);
1109
+ if (extension === ".docx")
1110
+ return "docx";
1111
+ if (extension === ".pptx")
1112
+ return "pptx";
1113
+ if (extension === ".pdf")
1114
+ return "pdf";
1115
+ if (extension === ".doc" || extension === ".ppt")
1116
+ return "legacy";
1117
+ return "unsupported";
1118
+ }
1119
+ function itemMetadataUrl(driveId, itemId) {
1120
+ return requestUrl(`${MICROSOFT_GRAPH_HOST}/drives/${encodeURIComponent(driveId)}/items/${encodeURIComponent(itemId)}`, { $select: "id,name,size,file,malware" });
1121
+ }
1122
+ function itemContentUrl(driveId, itemId, legacyPdfConversion) {
1123
+ return requestUrl(`${MICROSOFT_GRAPH_HOST}/drives/${encodeURIComponent(driveId)}/items/${encodeURIComponent(itemId)}/content`, legacyPdfConversion ? { format: "pdf" } : {});
1124
+ }
1125
+ // Shared retry-on-429 wrapper for the fetch path's Graph calls (metadata GET
1126
+ // and the initial content GET) — mirrors requestDeltaPageWithRetry's "honor
1127
+ // Retry-After exactly once, only when short enough to absorb inline" policy,
1128
+ // generalized to a plain Response instead of a parsed delta page (fetch also
1129
+ // needs the raw Response here: to read a redirect Location header, and to
1130
+ // stream the content body under a size cap, neither of which boundedJson's
1131
+ // JSON-only contract supports).
1132
+ async function requestWithRetry(transport, url, init, limits, sleep) {
1133
+ const first = await providerResponse(MICROSOFT, transport, url, init, limits);
1134
+ if (!first.ok)
1135
+ return first;
1136
+ if (first.value.status !== 429)
1137
+ return first;
1138
+ const retryAfterHeader = first.value.headers.get("retry-after");
1139
+ const retryAfterSeconds = retryAfterHeader !== null && /^\d+$/.test(retryAfterHeader)
1140
+ ? Number(retryAfterHeader)
1141
+ : undefined;
1142
+ if (retryAfterSeconds === undefined || retryAfterSeconds > MICROSOFT_MAX_RETRY_AFTER_SECONDS) {
1143
+ return err(new Error("Microsoft Graph request was rate limited"));
1144
+ }
1145
+ await sleep(retryAfterSeconds * 1000);
1146
+ const retried = await providerResponse(MICROSOFT, transport, url, init, limits);
1147
+ if (!retried.ok)
1148
+ return retried;
1149
+ if (retried.value.status === 429) {
1150
+ return err(new Error("Microsoft Graph request was rate limited"));
1151
+ }
1152
+ return retried;
1153
+ }
1154
+ // Reads a content Response body as raw bytes, bounded by
1155
+ // MICROSOFT_MAX_CONTENT_BYTES while streaming (not just trusting a declared
1156
+ // Content-Length) — mirrors boundedJson's streaming-cap shape but returns
1157
+ // bytes instead of parsed JSON, and reports a `too_large` SourceFailureReason
1158
+ // (not a generic Error) so a caller doesn't have to re-derive the reason.
1159
+ async function boundedContentBytes(response, limits) {
1160
+ const declaredLength = response.headers.get("content-length");
1161
+ if (declaredLength !== null && Number(declaredLength) > MICROSOFT_MAX_CONTENT_BYTES) {
1162
+ return err(taggedFetchError("too_large", "Microsoft Graph content response is too large"));
1163
+ }
1164
+ if (response.body === null) {
1165
+ return err(new Error("Microsoft Graph content response is invalid"));
1166
+ }
1167
+ const reader = response.body.getReader();
1168
+ let timeout;
1169
+ try {
1170
+ const read = async () => {
1171
+ const chunks = [];
1172
+ let length = 0;
1173
+ for (;;) {
1174
+ const next = await reader.read();
1175
+ if (next.done)
1176
+ break;
1177
+ length += next.value.byteLength;
1178
+ if (length > MICROSOFT_MAX_CONTENT_BYTES) {
1179
+ await reader.cancel();
1180
+ return err(taggedFetchError("too_large", "Microsoft Graph content response is too large"));
1181
+ }
1182
+ chunks.push(next.value);
1183
+ }
1184
+ return ok(new Uint8Array(Buffer.concat(chunks.map((chunk) => Buffer.from(chunk)), length)));
1185
+ };
1186
+ return await Promise.race([
1187
+ read(),
1188
+ new Promise((resolve) => {
1189
+ timeout = setTimeout(() => {
1190
+ void reader.cancel();
1191
+ resolve(err(new Error("Microsoft Graph content request failed")));
1192
+ }, limits.timeoutMilliseconds);
1193
+ }),
1194
+ ]);
1195
+ }
1196
+ catch {
1197
+ return err(new Error("Microsoft Graph content request failed"));
1198
+ }
1199
+ finally {
1200
+ if (timeout !== undefined)
1201
+ clearTimeout(timeout);
1202
+ }
1203
+ }
1204
+ // Resolves the per-enrollment includeSpeakerNotes flag (pptx-only; ignored
1205
+ // for docx/pdf) for a given source. A file enrollment is matched exactly by
1206
+ // driveId+remoteId (always unambiguous — a file enrollment names one specific
1207
+ // file, and its ref IS this source's id). A folder enrollment has no per-file
1208
+ // membership recorded anywhere reachable from here, so it's matched only when
1209
+ // exactly one folder enrollment shares this drive (an unambiguous best-effort
1210
+ // case); with more than one candidate, or none, this falls back to the
1211
+ // provider-level config default.
1212
+ function resolveIncludeSpeakerNotes(driveId, itemId, state, config) {
1213
+ const enrollments = state.enrollment ?? [];
1214
+ const fileMatch = enrollments.find((record) => record.kind === "file" && record.driveId === driveId && record.remoteId === itemId);
1215
+ if (fileMatch !== undefined)
1216
+ return fileMatch.includeSpeakerNotes;
1217
+ const folderMatches = enrollments.filter((record) => record.kind === "folder" && record.driveId === driveId);
1218
+ if (folderMatches.length === 1)
1219
+ return folderMatches[0].includeSpeakerNotes;
1220
+ return config.includeSpeakerNotes;
1221
+ }
1222
+ async function fetchSource(transport, limits, sleep, config, source, state) {
1223
+ const ids = splitSourceId(source.id);
1224
+ if (ids === undefined) {
1225
+ return err(taggedFetchError("fetch", "Microsoft source id is malformed"));
1226
+ }
1227
+ const { driveId, itemId } = ids;
1228
+ // Step 1 (R30): short-circuit a durable prior failure on this exact,
1229
+ // unchanged revision without downloading anything.
1230
+ const previous = state.sources[source.id];
1231
+ if (previous?.lastFailure !== undefined &&
1232
+ MICROSOFT_SHORT_CIRCUIT_REASONS.has(previous.lastFailure.reason) &&
1233
+ previous.revision === source.revision) {
1234
+ return err(taggedFetchError(previous.lastFailure.reason));
1235
+ }
1236
+ const accessToken = state.accessToken;
1237
+ // Step 2: metadata precheck (R29) — malware/size/type must be checked
1238
+ // BEFORE any content download is attempted.
1239
+ const metadataResponse = await requestWithRetry(transport, itemMetadataUrl(driveId, itemId), { headers: { authorization: `Bearer ${accessToken}` } }, limits, sleep);
1240
+ if (!metadataResponse.ok)
1241
+ return metadataResponse;
1242
+ if (metadataResponse.value.status === 403 || metadataResponse.value.status === 404) {
1243
+ return err(taggedFetchError("permission_revoked"));
1244
+ }
1245
+ if (metadataResponse.value.status < 200 || metadataResponse.value.status >= 300) {
1246
+ return err(taggedFetchError("fetch", `Microsoft Graph item metadata request failed with status ${metadataResponse.value.status}`));
1247
+ }
1248
+ const metadataBody = await boundedJson(MICROSOFT, metadataResponse.value, limits);
1249
+ if (!metadataBody.ok)
1250
+ return metadataBody;
1251
+ const metadata = metadataBody.value;
1252
+ // A truthy/object check, not just `!== undefined`: some Graph facets
1253
+ // serialize an absent facet as an explicit `null` rather than omitting the
1254
+ // key, and a bare `!== undefined` check would treat that `null` as
1255
+ // "malware facet present" — false-positiving every clean file as malware.
1256
+ if (typeof metadata.malware === "object" && metadata.malware !== null) {
1257
+ return err(taggedFetchError("malware"));
1258
+ }
1259
+ const size = typeof metadata.size === "number" ? metadata.size : undefined;
1260
+ if (size !== undefined && size > MICROSOFT_MAX_CONTENT_BYTES) {
1261
+ return err(taggedFetchError("too_large"));
1262
+ }
1263
+ const name = stringValue(metadata.name) ?? "";
1264
+ const classification = classifyExtension(name);
1265
+ if (classification === "unsupported") {
1266
+ return err(taggedFetchError("unsupported_type"));
1267
+ }
1268
+ const isLegacy = classification === "legacy";
1269
+ const kind = isLegacy ? "pdf" : classification;
1270
+ // Step 3 (R21, SECURITY): download the content. Graph 302s to a
1271
+ // short-lived pre-authenticated (SAS) URL; that redirect MUST be followed
1272
+ // WITHOUT the Authorization header — sending the Graph bearer token to the
1273
+ // SAS host would leak it. `redirect: "manual"` stops the transport from
1274
+ // auto-following, so the Location header can be read and re-requested with
1275
+ // an explicitly bare init (no authorization header at all).
1276
+ const initialContent = await requestWithRetry(transport, itemContentUrl(driveId, itemId, isLegacy), { headers: { authorization: `Bearer ${accessToken}` }, redirect: "manual" }, limits, sleep);
1277
+ if (!initialContent.ok)
1278
+ return initialContent;
1279
+ let contentResponse = initialContent.value;
1280
+ if (contentResponse.status >= 300 && contentResponse.status < 400) {
1281
+ const location = contentResponse.headers.get("location");
1282
+ if (location === null || location.length === 0) {
1283
+ return err(taggedFetchError("fetch", "Microsoft Graph content redirect is missing a Location header"));
1284
+ }
1285
+ // No Authorization header on this request — see the SECURITY note above.
1286
+ // Single-hop policy, deliberate: Graph's content endpoint is documented
1287
+ // to redirect exactly once to the SAS URL, so ONE hop is followed here
1288
+ // and no further. A SAS response that is ITSELF a redirect is unexpected
1289
+ // (not the normal shape) and is treated as a hard failure below rather
1290
+ // than followed again — this bounds the request chain and avoids ever
1291
+ // building an unbounded/looping redirect follow.
1292
+ //
1293
+ // Also deliberate: this uses plain providerResponse, not
1294
+ // requestWithRetry — a SAS blob-storage GET rate-limited (429) is not
1295
+ // retried the way the Graph-hosted metadata/content calls are; it's a
1296
+ // single-file download off a short-lived pre-authenticated URL, not a
1297
+ // Graph API subject to the same throttling contract.
1298
+ const redirected = await providerResponse(MICROSOFT, transport, location, {}, limits);
1299
+ if (!redirected.ok)
1300
+ return redirected;
1301
+ if (redirected.value.status >= 300 && redirected.value.status < 400) {
1302
+ return err(taggedFetchError("fetch", `Microsoft Graph content redirect chained to a second redirect (status ${redirected.value.status}), which is not followed`));
1303
+ }
1304
+ contentResponse = redirected.value;
1305
+ }
1306
+ if (contentResponse.status === 403 || contentResponse.status === 404) {
1307
+ return err(taggedFetchError("permission_revoked"));
1308
+ }
1309
+ if (contentResponse.status < 200 || contentResponse.status >= 300) {
1310
+ return err(taggedFetchError("fetch", `Microsoft Graph content request failed with status ${contentResponse.status}`));
1311
+ }
1312
+ const bytes = await boundedContentBytes(contentResponse, limits);
1313
+ if (!bytes.ok)
1314
+ return bytes;
1315
+ // Step 4/5: extract. Legacy `.doc`/`.ppt` bytes are already PDF (Graph did
1316
+ // the conversion for us) and run through the same pdf extractor as a
1317
+ // native .pdf — lossy (no speaker notes), which is expected (R28).
1318
+ const includeSpeakerNotes = resolveIncludeSpeakerNotes(driveId, itemId, state, config);
1319
+ const extracted = await extractText(bytes.value, kind, DEFAULT_EXTRACT_LIMITS, undefined, includeSpeakerNotes);
1320
+ if (!extracted.ok) {
1321
+ return err(taggedFetchError(extracted.error.reason, extracted.error.message));
1322
+ }
1323
+ // Step 6: echo the discovered revision (R21) so the engine's
1324
+ // fetched.value.revision === remote.revision equality check passes.
1325
+ const normalized = {
1326
+ id: source.id,
1327
+ revision: source.revision,
1328
+ text: extracted.value.text,
1329
+ ...(isLegacy ? { extractor: "pdf-conversion" } : {}),
1330
+ };
1331
+ return ok(normalized);
1332
+ }
1333
+ // ---------------------------------------------------------------------------
1334
+ // Enrollment (U17): resolveEnrollment + estimateEnrollment (R11, R12, R13,
1335
+ // R33, R39). `selection` is an UNTRUSTED File-Picker payload — every
1336
+ // referenced item is re-fetched with the server's own delegated token and
1337
+ // validated (readable, supported type) before it can ever reach the draft.
1338
+ // Folder expansion reuses U14's delta-walk primitives (walkMicrosoftDeltaPages
1339
+ // + containerInitUrl/containerFallbackUrl + foldContainerAncestry) rather than
1340
+ // duplicating them; it runs a one-shot, non-cursored walk (this is a resolve-
1341
+ // time/estimate-time preview, not the persisted discovery cursor).
1342
+ // §5.3 bounds: a single container may not expand past this many eligible
1343
+ // files (enroll sub-folders instead of one giant one), and — see the note on
1344
+ // MICROSOFT_MAX_ENROLLED_SOURCES below — this Microsoft connector may not
1345
+ // carry more than this many enrolled sources of its own.
1346
+ const MICROSOFT_MAX_ELIGIBLE_PER_CONTAINER = 1_000;
1347
+ // PER-PROVIDER, not a cross-vault total: resolveEnrollment only ever sees
1348
+ // this Microsoft adapter's own ProviderState (the adapter interface has no
1349
+ // visibility into Google's or Notion's ProviderState), so this bound is
1350
+ // checked against `Object.keys(state.sources).length` — i.e. sources this
1351
+ // Microsoft connector has already discovered — plus this resolve's newly
1352
+ // eligible count. A vault with, say, 1,999 Google sources AND 1,999
1353
+ // Microsoft sources passes this check even though it's nowhere near
1354
+ // "2,000 sources total"; the design rationale is staying comfortably under
1355
+ // the ENGINE's own per-provider reconcile limit (DEFAULT_RECONCILE_LIMITS.
1356
+ // maxSources = 10,000 in engine.ts), which is itself per-provider. A true
1357
+ // cross-provider vault-wide cap would need to read the full IntegrationState
1358
+ // (every provider's ProviderState) at the engine/route layer, not from
1359
+ // inside a single adapter — out of scope here; a bead tracks that decision.
1360
+ const MICROSOFT_MAX_ENROLLED_SOURCES = 2_000;
1361
+ // Measured docx/pptx/pdf text_chars/source_bytes ratios (U10,
1362
+ // test/fixtures/extract/ratios.json). Copied here as named constants — this
1363
+ // module must NOT read that file at runtime (it's a test artifact); a
1364
+ // drift-guard test in microsoft.test.ts asserts these equal the fixture.
1365
+ // Exported (not just module-private) so microsoft.test.ts's drift-guard test
1366
+ // asserts against the actual constant this module computes with, rather than
1367
+ // a second hardcoded copy that could silently diverge from it.
1368
+ export const MICROSOFT_ESTIMATE_RATIOS = {
1369
+ docx: 0.12354847077613262,
1370
+ pptx: 0.07532872743481168,
1371
+ pdf: 0.10687655343827672,
1372
+ };
1373
+ function enrollmentItemMetadataUrl(driveId, itemId) {
1374
+ return requestUrl(`${MICROSOFT_GRAPH_HOST}/drives/${encodeURIComponent(driveId)}/items/${encodeURIComponent(itemId)}`, { $select: "id,name,eTag,file,folder,size,webUrl,sharepointIds,parentReference,malware" });
1375
+ }
1376
+ async function fetchEnrollmentItemMetadata(transport, accessToken, limits, driveId, itemId) {
1377
+ return requestJson(transport, enrollmentItemMetadataUrl(driveId, itemId), { headers: { authorization: `Bearer ${accessToken}` } }, limits);
1378
+ }
1379
+ // Parses one untrusted picker-payload entry into a bare {driveId, itemId}
1380
+ // reference — nothing here is trusted; it only shapes what gets re-fetched
1381
+ // from Graph next. Accepts either `id` or `itemId` as the item id key (File
1382
+ // Picker v8 payloads vary on this).
1383
+ function parsePickerRef(raw) {
1384
+ if (typeof raw !== "object" || raw === null)
1385
+ return undefined;
1386
+ const obj = raw;
1387
+ const driveId = stringValue(obj.driveId);
1388
+ const itemId = stringValue(obj.itemId) ?? stringValue(obj.id);
1389
+ if (driveId === undefined || itemId === undefined)
1390
+ return undefined;
1391
+ return { driveId, itemId };
1392
+ }
1393
+ function pickerRefFallbackName(raw) {
1394
+ if (typeof raw !== "object" || raw === null)
1395
+ return "unknown";
1396
+ const obj = raw;
1397
+ return stringValue(obj.name) ?? stringValue(obj.itemId) ?? stringValue(obj.id) ?? "unknown";
1398
+ }
1399
+ // One-shot folder expansion (resolve/estimate time, not the persisted
1400
+ // discovery cursor from U14): walks the folder-scoped delta once (falling
1401
+ // back to the drive-root delta + ancestry fold on a 400, exactly as
1402
+ // walkContainerRoot's freshWalk does for discovery), then filters the
1403
+ // resulting item set down to in-subtree, non-deleted files with a supported
1404
+ // extension.
1405
+ async function walkContainerChildren(transport, accessToken, limits, sleep, driveId, folderId) {
1406
+ const primary = await walkMicrosoftDeltaPages(transport, accessToken, limits, sleep, containerInitUrl(driveId, folderId));
1407
+ let items;
1408
+ let ancestorIds;
1409
+ if (primary.ok) {
1410
+ items = primary.value.items;
1411
+ }
1412
+ else if (isBadRequestError(primary.error)) {
1413
+ const fallback = await walkMicrosoftDeltaPages(transport, accessToken, limits, sleep, containerFallbackUrl(driveId));
1414
+ if (!fallback.ok)
1415
+ return fallback;
1416
+ ancestorIds = foldContainerAncestry(fallback.value.items, folderId, []);
1417
+ items = fallback.value.items;
1418
+ }
1419
+ else {
1420
+ return primary;
1421
+ }
1422
+ const eligible = [];
1423
+ let legacyCount = 0;
1424
+ for (const raw of items) {
1425
+ if (typeof raw !== "object" || raw === null)
1426
+ continue;
1427
+ const item = raw;
1428
+ const id = deltaItemId(raw);
1429
+ if (id === undefined || id === folderId)
1430
+ continue;
1431
+ if (item.deleted !== undefined || item.folder !== undefined || item.file === undefined) {
1432
+ continue;
1433
+ }
1434
+ if (ancestorIds !== undefined) {
1435
+ const parentId = deltaParentId(item);
1436
+ if (parentId === undefined || !ancestorIds.has(parentId))
1437
+ continue;
1438
+ }
1439
+ // Same defense-in-depth malware exclusion as the top-level item check
1440
+ // above — a flagged child is silently excluded from the eligible set
1441
+ // (fetchSource's own R29 precheck is the authoritative gate at fetch
1442
+ // time; this just keeps a malware-flagged file out of the cost preview).
1443
+ if (typeof item.malware === "object" && item.malware !== null)
1444
+ continue;
1445
+ const name = stringValue(item.name) ?? "";
1446
+ const classification = classifyExtension(name);
1447
+ if (classification === "unsupported")
1448
+ continue;
1449
+ if (classification === "legacy")
1450
+ legacyCount += 1;
1451
+ const size = typeof item.size === "number" ? item.size : 0;
1452
+ eligible.push({ id, name, size });
1453
+ }
1454
+ return ok({ eligible, legacyCount });
1455
+ }
1456
+ function validateEnrollmentItemMetadata(body, name) {
1457
+ const isFile = typeof body.file === "object" && body.file !== null;
1458
+ if (!isFile)
1459
+ return { ok: false, reason: "unsupported_type" };
1460
+ // Same truthy/object malware check as fetchSource's R29 precheck (a `null`
1461
+ // malware facet must not false-positive) — defense in depth at
1462
+ // enrollment/estimate time too, not just on every subsequent fetch cycle.
1463
+ if (typeof body.malware === "object" && body.malware !== null) {
1464
+ return { ok: false, reason: "malware" };
1465
+ }
1466
+ if (classifyExtension(name) === "unsupported") {
1467
+ return { ok: false, reason: "unsupported_type" };
1468
+ }
1469
+ const size = typeof body.size === "number" ? body.size : 0;
1470
+ return { ok: true, size };
1471
+ }
1472
+ function readersForCollection(roles, collection) {
1473
+ if (roles === undefined)
1474
+ return [];
1475
+ return Object.keys(roles)
1476
+ .filter((name) => canRead(roles[name], collection))
1477
+ .sort();
1478
+ }
1479
+ function ratifiersForCollection(roles) {
1480
+ if (roles === undefined)
1481
+ return [];
1482
+ return Object.keys(roles)
1483
+ .filter((name) => canRatify(roles[name]))
1484
+ .sort();
1485
+ }
1486
+ async function resolveEnrollment(transport, limits, sleep, roles, selection, state, ctx) {
1487
+ if (!Array.isArray(selection) || selection.length === 0) {
1488
+ return err(new Error("Microsoft enrollment selection must be a non-empty array"));
1489
+ }
1490
+ const items = [];
1491
+ const skipped = [];
1492
+ let newEligibleTotal = 0;
1493
+ // A picker payload can reference the same item twice (accidental
1494
+ // double-click, a forged duplicate, ...); dedupe by driveId:itemId so a
1495
+ // repeat neither re-fetches metadata nor double-counts toward the eligible
1496
+ // bound below. Silent, not a `skipped` entry — the item IS being enrolled,
1497
+ // just once.
1498
+ const seenRefs = new Set();
1499
+ for (const raw of selection) {
1500
+ const ref = parsePickerRef(raw);
1501
+ if (ref === undefined) {
1502
+ skipped.push({ name: pickerRefFallbackName(raw), reason: "invalid_reference" });
1503
+ continue;
1504
+ }
1505
+ const refKey = `${ref.driveId}:${ref.itemId}`;
1506
+ if (seenRefs.has(refKey))
1507
+ continue;
1508
+ seenRefs.add(refKey);
1509
+ const metadata = await fetchEnrollmentItemMetadata(transport, state.accessToken, limits, ref.driveId, ref.itemId);
1510
+ // A transport-level failure (network/timeout) is never a positive
1511
+ // "unreadable" signal — this can't be confirmed at all, so it fails the
1512
+ // whole resolve rather than silently skipping the item (R12).
1513
+ if (!metadata.ok)
1514
+ return metadata;
1515
+ if (metadata.value.status === 403 || metadata.value.status === 404) {
1516
+ // The picker-supplied `name` is untrusted (just a display label the
1517
+ // caller can't use to bypass the metadata check above), but it's fine
1518
+ // to echo it back into the skip list purely for identification.
1519
+ skipped.push({ name: pickerRefFallbackName(raw), reason: "not_readable" });
1520
+ continue;
1521
+ }
1522
+ if (metadata.value.status < 200 || metadata.value.status >= 300) {
1523
+ return err(new Error(`Microsoft Graph enrollment metadata request failed with status ${metadata.value.status}`));
1524
+ }
1525
+ const body = metadata.value.body;
1526
+ const name = stringValue(body.name) ?? ref.itemId;
1527
+ const webUrl = stringValue(body.webUrl);
1528
+ const isFolder = typeof body.folder === "object" && body.folder !== null;
1529
+ if (isFolder) {
1530
+ const expanded = await walkContainerChildren(transport, state.accessToken, limits, sleep, ref.driveId, ref.itemId);
1531
+ if (!expanded.ok)
1532
+ return expanded;
1533
+ if (expanded.value.eligible.length > MICROSOFT_MAX_ELIGIBLE_PER_CONTAINER) {
1534
+ return err(new Error(`Microsoft folder "${name}" has ${expanded.value.eligible.length} eligible files ` +
1535
+ `(limit ${MICROSOFT_MAX_ELIGIBLE_PER_CONTAINER}); enroll sub-folders instead`));
1536
+ }
1537
+ newEligibleTotal += expanded.value.eligible.length;
1538
+ items.push({
1539
+ driveId: ref.driveId,
1540
+ remoteId: ref.itemId,
1541
+ kind: "container",
1542
+ label: name,
1543
+ ...(webUrl === undefined ? {} : { webUrl }),
1544
+ // Cached so estimateEnrollment can skip re-walking this folder's
1545
+ // delta for the common resolve-then-estimate preview flow.
1546
+ children: expanded.value.eligible,
1547
+ });
1548
+ continue;
1549
+ }
1550
+ const validation = validateEnrollmentItemMetadata(body, name);
1551
+ if (!validation.ok) {
1552
+ skipped.push({ name, reason: validation.reason });
1553
+ continue;
1554
+ }
1555
+ newEligibleTotal += 1;
1556
+ items.push({
1557
+ driveId: ref.driveId,
1558
+ remoteId: ref.itemId,
1559
+ kind: "item",
1560
+ label: name,
1561
+ ...(webUrl === undefined ? {} : { webUrl }),
1562
+ // Cached so estimateEnrollment can skip re-fetching this item's
1563
+ // metadata for the common resolve-then-estimate preview flow.
1564
+ size: validation.size,
1565
+ });
1566
+ }
1567
+ // See the MICROSOFT_MAX_ENROLLED_SOURCES comment above: this is a
1568
+ // per-Microsoft-connector count (this adapter's own state.sources), not a
1569
+ // cross-provider vault total.
1570
+ const existingSourceCount = Object.keys(state.sources).length;
1571
+ if (existingSourceCount + newEligibleTotal > MICROSOFT_MAX_ENROLLED_SOURCES) {
1572
+ return err(new Error(`Microsoft enrollment would exceed the ${MICROSOFT_MAX_ENROLLED_SOURCES}-source ` +
1573
+ `Microsoft-enrolled-source limit (${existingSourceCount} existing + ${newEligibleTotal} new)`));
1574
+ }
1575
+ return ok({
1576
+ items,
1577
+ collection: ctx.collection,
1578
+ includeSpeakerNotes: ctx.includeSpeakerNotes,
1579
+ readersAtEnrollment: readersForCollection(roles, ctx.collection),
1580
+ skipped,
1581
+ });
1582
+ }
1583
+ async function estimateEnrollment(transport, limits, sleep, roles, estimatedUsdPerCall, draft, state) {
1584
+ const skipped = [...draft.skipped];
1585
+ const byType = { docx: 0, pptx: 0, pdf: 0 };
1586
+ let bytes = 0;
1587
+ let eligible = 0;
1588
+ let legacyCount = 0;
1589
+ let estimatedCallsTotal = 0;
1590
+ const accumulate = (name, size) => {
1591
+ const classification = classifyExtension(name);
1592
+ if (classification === "unsupported")
1593
+ return;
1594
+ const bucket = classification === "legacy" ? "pdf" : classification;
1595
+ if (classification === "legacy")
1596
+ legacyCount += 1;
1597
+ const cappedSize = Math.min(size, MICROSOFT_MAX_CONTENT_BYTES);
1598
+ const estChars = cappedSize * MICROSOFT_ESTIMATE_RATIOS[bucket];
1599
+ const calls = Math.min(DISTILL_NUMERIC_DEFAULTS.maxLlmCalls, Math.ceil(estChars / DISTILL_NUMERIC_DEFAULTS.inCallInputCap));
1600
+ bytes += cappedSize;
1601
+ byType[bucket] += cappedSize;
1602
+ estimatedCallsTotal += calls;
1603
+ eligible += 1;
1604
+ };
1605
+ for (const item of draft.items) {
1606
+ if (item.kind === "item") {
1607
+ // Common path: resolveEnrollment already validated and cached this
1608
+ // item's size on the draft — accumulate from it directly, no Graph
1609
+ // call. Re-validating (malware/type) here would only repeat work
1610
+ // resolveEnrollment already did against the same, unchanged draft.
1611
+ if (item.size !== undefined) {
1612
+ accumulate(item.label, item.size);
1613
+ continue;
1614
+ }
1615
+ // Stateless fallback: a caller can POST a bare EnrollmentDraft (no
1616
+ // cached size) straight to an /estimate route without a matching
1617
+ // resolve call first — re-fetch and re-validate in that case.
1618
+ const metadata = await fetchEnrollmentItemMetadata(transport, state.accessToken, limits, item.driveId, item.remoteId);
1619
+ if (!metadata.ok)
1620
+ return metadata;
1621
+ if (metadata.value.status === 403 || metadata.value.status === 404) {
1622
+ skipped.push({ name: item.label, reason: "not_readable" });
1623
+ continue;
1624
+ }
1625
+ if (metadata.value.status < 200 || metadata.value.status >= 300) {
1626
+ return err(new Error(`Microsoft Graph enrollment metadata request failed with status ${metadata.value.status}`));
1627
+ }
1628
+ const body = metadata.value.body;
1629
+ const name = stringValue(body.name) ?? item.label;
1630
+ const validation = validateEnrollmentItemMetadata(body, name);
1631
+ if (!validation.ok) {
1632
+ skipped.push({ name, reason: validation.reason });
1633
+ continue;
1634
+ }
1635
+ accumulate(name, validation.size);
1636
+ continue;
1637
+ }
1638
+ // Common path: resolveEnrollment already expanded and cached this
1639
+ // container's eligible children on the draft — no delta re-walk needed.
1640
+ if (item.children !== undefined) {
1641
+ for (const child of item.children)
1642
+ accumulate(child.name, child.size);
1643
+ continue;
1644
+ }
1645
+ // Stateless fallback (see the item branch above): re-walk when the
1646
+ // draft carries no cached children.
1647
+ const expanded = await walkContainerChildren(transport, state.accessToken, limits, sleep, item.driveId, item.remoteId);
1648
+ if (!expanded.ok)
1649
+ return expanded;
1650
+ for (const child of expanded.value.eligible)
1651
+ accumulate(child.name, child.size);
1652
+ }
1653
+ const expected = estimatedCallsTotal;
1654
+ const low = Math.floor(expected * 0.8);
1655
+ const high = Math.ceil(expected * 1.2);
1656
+ const warnings = [];
1657
+ if (legacyCount > 0) {
1658
+ warnings.push(`${legacyCount} legacy .ppt/.doc file${legacyCount === 1 ? "" : "s"} will use lossy PDF conversion`);
1659
+ }
1660
+ if (roles === undefined) {
1661
+ warnings.push("RBAC roles not supplied; readers/ratifiers omitted");
1662
+ }
1663
+ return ok({
1664
+ eligible,
1665
+ skipped,
1666
+ bytes,
1667
+ byType,
1668
+ estimatedCalls: { low, expected, high },
1669
+ ...(estimatedUsdPerCall === undefined
1670
+ ? {}
1671
+ : { estimatedUsd: { expected: expected * estimatedUsdPerCall } }),
1672
+ collection: draft.collection,
1673
+ readers: readersForCollection(roles, draft.collection),
1674
+ ratifiers: ratifiersForCollection(roles),
1675
+ warnings,
1676
+ });
1677
+ }
1678
+ // U18: pure status projection over ProviderState (design §13). No Graph
1679
+ // calls, no I/O — everything here is a straight read of already-persisted
1680
+ // fields. Two known gaps, both flagged rather than faked:
1681
+ //
1682
+ // 1. `webhook.subscriptions` has no per-subscription health/error field, so
1683
+ // a "degraded" (partial-failure) ProviderWebhookStatus can't be derived
1684
+ // from state alone — this only ever reports "off" or "active".
1685
+ // 2. SourceState.enrollmentId is not populated by the engine for any
1686
+ // adapter yet (see the rememberedRootSources comment above), so grouping
1687
+ // sources under their enrollment is correct but, until that plumbing
1688
+ // lands, every enrollment's sourceCount/failedSourceCount will read 0.
1689
+ // The §13 per-source lifecycle state, derived from the real SourceState
1690
+ // fields persisted by the engine (contentHash/available/lastFailure) — no
1691
+ // field is invented. Order matters: unavailable and over_limit both take
1692
+ // precedence over a plain "failed" classification.
1693
+ function microsoftSourceStatusState(source) {
1694
+ if (!source.available)
1695
+ return "unavailable";
1696
+ if (source.lastFailure !== undefined) {
1697
+ return source.lastFailure.reason === "limit" ? "over_limit" : "failed";
1698
+ }
1699
+ return source.contentHash.length > 0 ? "current" : "pending";
1700
+ }
1701
+ function describeMicrosoftStatus(state) {
1702
+ const connection = state.authorization?.status === "reconnect_required"
1703
+ ? {
1704
+ kind: "reconnect_required",
1705
+ reason: state.authorization.reason ?? "Microsoft authorization was revoked",
1706
+ }
1707
+ : state.account !== undefined
1708
+ ? { kind: "connected", account: state.account }
1709
+ : { kind: "disconnected" };
1710
+ const subscriptions = state.webhook?.subscriptions ?? [];
1711
+ const webhook = subscriptions.length > 0
1712
+ ? {
1713
+ kind: "active",
1714
+ eventCount: subscriptions.length,
1715
+ earliestExpiry: earliestSubscriptionExpiry(subscriptions),
1716
+ }
1717
+ : { kind: "off" };
1718
+ const allSources = Object.values(state.sources);
1719
+ // Derived from SOURCE_STATUS_STATES rather than hand-listed, so a sixth
1720
+ // state added to the union can't be silently missed here.
1721
+ const emptyCounts = Object.fromEntries(SOURCE_STATUS_STATES.map((sourceStatusState) => [sourceStatusState, 0]));
1722
+ const enrollments = (state.enrollment ?? []).map((record) => {
1723
+ // A file enrollment's ref IS its source id; a folder's descendants carry
1724
+ // different ids and are not persisted with an enrollment link, so they
1725
+ // aggregate under the availability sweep rather than here (sourceCount 0).
1726
+ const enrolledSources = allSources.filter((source) => source.id === record.ref);
1727
+ const counts = { ...emptyCounts };
1728
+ for (const source of enrolledSources)
1729
+ counts[microsoftSourceStatusState(source)] += 1;
1730
+ return {
1731
+ id: record.ref,
1732
+ label: record.label,
1733
+ collection: record.targetCollection,
1734
+ sourceCount: enrolledSources.length,
1735
+ failedSourceCount: enrolledSources.filter((source) => source.lastFailure !== undefined)
1736
+ .length,
1737
+ counts,
1738
+ };
1739
+ });
1740
+ const sources = allSources.map((source) => ({
1741
+ id: source.id,
1742
+ available: source.available,
1743
+ lastSeenAt: source.lastSeenAt,
1744
+ ...(source.lastFailure === undefined ? {} : { lastFailure: source.lastFailure }),
1745
+ state: microsoftSourceStatusState(source),
1746
+ }));
1747
+ return {
1748
+ connection,
1749
+ webhook,
1750
+ enrollments,
1751
+ sources,
1752
+ sensitivityLabels: "not checked (V1)",
1753
+ };
1754
+ }
1755
+ export function createMicrosoftAdapter(options) {
1756
+ const { config, redirectUri } = options;
1757
+ const transport = options.transport ?? globalThis.fetch;
1758
+ const now = options.now ?? (() => new Date());
1759
+ const limits = {
1760
+ timeoutMilliseconds: options.requestTimeoutMilliseconds ?? DEFAULT_REQUEST_TIMEOUT_MILLISECONDS,
1761
+ maxResponseBytes: options.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES,
1762
+ };
1763
+ return {
1764
+ name: "m365",
1765
+ webhookSetup: "automatic",
1766
+ authorizationUrl: (input) => authorizationUrl(input, redirectUri, config),
1767
+ exchangeCode: (input) => exchangeCode(transport, redirectUri, now, config, limits, input),
1768
+ refreshTokens: (input) => refreshTokens(transport, now, config, limits, input),
1769
+ ensureWebhook: (state, input) => ensureWebhook(transport, state, input, limits),
1770
+ verifyWebhook: (input, state) => verifyWebhook(input, state, now),
1771
+ answerWebhookChallenge,
1772
+ verifyLifecycleWebhook: (input, state) => verifyLifecycleWebhook(input, state, now),
1773
+ discover: (state) => discoverMicrosoftSources(transport, limits, defaultSleep, state),
1774
+ fetch: (source, state) => fetchSource(transport, limits, defaultSleep, config, source, state),
1775
+ resolveEnrollment: (selection, state, ctx) => resolveEnrollment(transport, limits, defaultSleep, options.roles, selection, state, ctx),
1776
+ estimateEnrollment: (draft, state) => estimateEnrollment(transport, limits, defaultSleep, options.roles, options.estimatedUsdPerCall, draft, state),
1777
+ describeStatus: (state) => describeMicrosoftStatus(state),
1778
+ };
1779
+ }
1780
+ //# sourceMappingURL=microsoft.js.map