@tearleads/client-sdk 0.1.42 → 0.1.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/containerDocumentDiscovery.js +71 -29
- package/client/containerDocumentDiscovery.js.map +1 -1
- package/client/deviceFirst.js +4 -5
- package/client/deviceFirst.js.map +1 -1
- package/data/blobs/transferPool.js +35 -8
- package/data/blobs/transferPool.js.map +1 -1
- package/data/containers/shared/projection.js +12 -0
- package/data/containers/shared/projection.js.map +1 -1
- package/data/documents/blob/shared/types.d.ts +2 -3
- package/data/documents/blob/shared/types.js.map +1 -1
- package/data/documents/shared/events.js +0 -1
- package/data/documents/shared/events.js.map +1 -1
- package/data/persistence/container-contents/containerContentsPersistence.js +6 -22
- package/data/persistence/container-contents/containerContentsPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js +5 -15
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerPendingUpdatePersistence.js +6 -15
- package/data/persistence/container-contents/containerPendingUpdatePersistence.js.map +1 -1
- package/data/persistence/container-contents/containerRecoveryPersistence.js +3 -4
- package/data/persistence/container-contents/containerRecoveryPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerRemovalWork.js +3 -5
- package/data/persistence/container-contents/containerRemovalWork.js.map +1 -1
- package/data/persistence/container-contents/dormantContainerMetadata.js +1 -5
- package/data/persistence/container-contents/dormantContainerMetadata.js.map +1 -1
- package/data/persistence/containers/containerSyncWatermarkPersistence.d.ts +11 -3
- package/data/persistence/containers/containerSyncWatermarkPersistence.js +46 -58
- package/data/persistence/containers/containerSyncWatermarkPersistence.js.map +1 -1
- package/data/persistence/documents/documentsPersistence.js +4 -9
- package/data/persistence/documents/documentsPersistence.js.map +1 -1
- package/data/persistence/documents/internal/discardDocument.js +1 -2
- package/data/persistence/documents/internal/discardDocument.js.map +1 -1
- package/data/persistence/documents/internal/documentDeletionPersistence.js +1 -2
- package/data/persistence/documents/internal/documentDeletionPersistence.js.map +1 -1
- package/data/persistence/documents/internal/documentMutationPersistence.js +17 -25
- package/data/persistence/documents/internal/documentMutationPersistence.js.map +1 -1
- package/data/persistence/documents/internal/orphanSideRows.js +2 -14
- package/data/persistence/documents/internal/orphanSideRows.js.map +1 -1
- package/data/persistence/documents/internal/syncQueuePersistence.js +3 -3
- package/data/persistence/documents/internal/syncQueuePersistence.js.map +1 -1
- package/data/sqlite/documentHistoryPersistence.d.ts +9 -3
- package/data/sqlite/documentHistoryPersistence.js +31 -8
- package/data/sqlite/documentHistoryPersistence.js.map +1 -1
- package/data/sqlite/documentPendingUpdatePersistence.d.ts +58 -11
- package/data/sqlite/documentPendingUpdatePersistence.js +137 -63
- package/data/sqlite/documentPendingUpdatePersistence.js.map +1 -1
- package/data/sqlite/documentPersistence.d.ts +1 -1
- package/data/sqlite/documentPersistence.js +1 -1
- package/data/sqlite/documentPersistence.js.map +1 -1
- package/data/sqlite/documentPersistenceTypes.d.ts +2 -2
- package/data/sqlite/documentPersistenceTypes.js.map +1 -1
- package/data/sqlite/documentRecordPersistence.js +5 -4
- package/data/sqlite/documentRecordPersistence.js.map +1 -1
- package/data/sqlite/schema.d.ts +167 -517
- package/data/sqlite/schema.js +68 -82
- package/data/sqlite/schema.js.map +1 -1
- package/internal/api-client/ApiClient.d.ts +18 -41
- package/internal/api-client/ApiClient.js +9 -14
- package/internal/api-client/ApiClient.js.map +1 -1
- package/internal/api-client/apiRequestRuntime.d.ts +2 -0
- package/internal/api-client/apiRequestRuntime.js +4 -0
- package/internal/api-client/apiRequestRuntime.js.map +1 -1
- package/internal/api-client/binaryRequestOperationTransport.d.ts +18 -0
- package/internal/api-client/index.d.ts +1 -0
- package/internal/api-client/index.js +1 -0
- package/internal/api-client/index.js.map +1 -1
- package/internal/api-client/operationTransportFactory.d.ts +6 -0
- package/internal/api-client/requestInternals.js +7 -7
- package/internal/api-client/requestInternals.js.map +1 -1
- package/internal/api-client/routes/blobs/get.d.ts +7 -0
- package/internal/api-client/routes/blobs/get.js.map +1 -1
- package/internal/api-client/routes/blobs/multipart.js +1 -11
- package/internal/api-client/routes/blobs/multipart.js.map +1 -1
- package/internal/api-client/routes/blobs/multipartUpload.d.ts +14 -0
- package/internal/api-client/routes/blobs/multipartUpload.js +94 -43
- package/internal/api-client/routes/blobs/multipartUpload.js.map +1 -1
- package/internal/api-client/routes/containers/reads.d.ts +13 -7
- package/internal/api-client/routes/containers/reads.js +36 -15
- package/internal/api-client/routes/containers/reads.js.map +1 -1
- package/internal/api-client/types.d.ts +1 -5
- package/internal/api-client/types.js.map +1 -1
- package/internal/validators/containerReadRefinements.js +15 -5
- package/internal/validators/containerReadRefinements.js.map +1 -1
- package/internal/validators/containerReadSchemas.d.ts +36 -0
- package/internal/validators/containerReadSchemas.js +64 -1
- package/internal/validators/containerReadSchemas.js.map +1 -1
- package/internal/validators/operation/attachments.d.ts +80 -160
- package/internal/validators/operation/auth.d.ts +760 -1664
- package/internal/validators/operation/blobDownloads.js +1 -1
- package/internal/validators/operation/blobDownloads.js.map +1 -1
- package/internal/validators/operation/containerMutations.d.ts +416 -856
- package/internal/validators/operation/containerReads.d.ts +118 -182
- package/internal/validators/operation/containerReads.js +20 -54
- package/internal/validators/operation/containerReads.js.map +1 -1
- package/internal/validators/operation/documentMutations.d.ts +120 -264
- package/internal/validators/operation/documentSync.d.ts +40 -80
- package/internal/validators/operation/index.d.ts +2 -2
- package/internal/validators/operation/index.js +2 -2
- package/internal/validators/operation/index.js.map +1 -1
- package/internal/validators/operation/multipartBlobs.d.ts +70 -190
- package/internal/validators/operation/multipartBlobs.js +2 -24
- package/internal/validators/operation/multipartBlobs.js.map +1 -1
- package/internal/validators/operation/multipartPartUploads.d.ts +5 -5
- package/internal/validators/operation/multipartPartUploads.js +6 -20
- package/internal/validators/operation/multipartPartUploads.js.map +1 -1
- package/internal/validators/operation/organizations.d.ts +882 -1906
- package/internal/validators/operation/principals.d.ts +120 -240
- package/internal/validators/operation/registry.d.ts +3 -4
- package/internal/validators/operation/registry.js +3 -4
- package/internal/validators/operation/registry.js.map +1 -1
- package/internal/validators/request/blob.d.ts +100 -201
- package/internal/validators/request/blob.js +6 -13
- package/internal/validators/request/blob.js.map +1 -1
- package/internal/validators/request/container.d.ts +136 -272
- package/internal/validators/request/container.js +16 -10
- package/internal/validators/request/container.js.map +1 -1
- package/internal/validators/request/containerDocumentLanes.d.ts +23 -0
- package/internal/validators/request/containerDocumentLanes.js +23 -0
- package/internal/validators/request/containerDocumentLanes.js.map +1 -0
- package/internal/validators/request/containerManifestRef.d.ts +54 -0
- package/internal/validators/request/containerManifestRef.js +28 -0
- package/internal/validators/request/containerManifestRef.js.map +1 -0
- package/internal/validators/request/containerMetadata.d.ts +160 -368
- package/internal/validators/request/containerParentLanes.js +5 -35
- package/internal/validators/request/containerParentLanes.js.map +1 -1
- package/internal/validators/request/document.d.ts +100 -245
- package/internal/validators/request/document.js +1 -2
- package/internal/validators/request/document.js.map +1 -1
- package/internal/validators/request/documentSyncSchema.d.ts +42 -109
- package/internal/validators/request/documentSyncSchema.js +8 -25
- package/internal/validators/request/documentSyncSchema.js.map +1 -1
- package/internal/validators/request/index.d.ts +2 -1
- package/internal/validators/request/index.js +2 -1
- package/internal/validators/request/index.js.map +1 -1
- package/internal/validators/request/organization.d.ts +160 -320
- package/internal/validators/request/organizationProvisioning.d.ts +2424 -5312
- package/internal/validators/request/principal.d.ts +160 -320
- package/internal/validators/request/registration.d.ts +760 -1664
- package/internal/validators/response/blob.d.ts +48 -27
- package/internal/validators/response/blob.js +3 -10
- package/internal/validators/response/blob.js.map +1 -1
- package/internal/validators/response/containerDocumentLanes.d.ts +82 -0
- package/internal/validators/response/containerDocumentLanes.js +35 -0
- package/internal/validators/response/containerDocumentLanes.js.map +1 -0
- package/internal/validators/response/containerParentLanes.js +3 -15
- package/internal/validators/response/containerParentLanes.js.map +1 -1
- package/internal/validators/response/index.d.ts +3 -2
- package/internal/validators/response/index.js +2 -2
- package/internal/validators/response/index.js.map +1 -1
- package/internal/validators/util/blob.d.ts +7 -0
- package/internal/validators/util/blob.js +7 -0
- package/internal/validators/util/blob.js.map +1 -1
- package/internal/validators/util/blobTransferSchemas.d.ts +41 -0
- package/internal/validators/util/blobTransferSchemas.js +39 -0
- package/internal/validators/util/blobTransferSchemas.js.map +1 -0
- package/internal/validators/util/containerLimits.d.ts +7 -0
- package/internal/validators/util/containerLimits.js +7 -0
- package/internal/validators/util/containerLimits.js.map +1 -1
- package/internal/validators/util/index.d.ts +2 -1
- package/internal/validators/util/index.js +2 -1
- package/internal/validators/util/index.js.map +1 -1
- package/package.json +1 -1
- package/sync/reconciliation/knownContainerSweep.js +57 -54
- package/sync/reconciliation/knownContainerSweep.js.map +1 -1
- package/sync/reconciliation/reconcileContainer.js +49 -17
- package/sync/reconciliation/reconcileContainer.js.map +1 -1
- package/sync/reconciliation/service.js +44 -16
- package/sync/reconciliation/service.js.map +1 -1
- package/sync/reconciliation/serviceTypes.d.ts +23 -2
- package/sync/reconciliation/serviceTypes.js.map +1 -1
- package/workflows/blobs/multipartFirstParts.js +82 -0
- package/workflows/blobs/multipartFirstParts.js.map +1 -0
- package/workflows/blobs/multipartStageResolution.js +83 -0
- package/workflows/blobs/multipartStageResolution.js.map +1 -0
- package/workflows/blobs/multipartUpload.js +45 -89
- package/workflows/blobs/multipartUpload.js.map +1 -1
- package/workflows/container-contents/containerDocumentListing.js +92 -38
- package/workflows/container-contents/containerDocumentListing.js.map +1 -1
- package/workflows/container-contents/containerPersistence.d.ts +8 -2
- package/workflows/container-contents/containerPersistence.js +6 -7
- package/workflows/container-contents/containerPersistence.js.map +1 -1
- package/workflows/container-contents/documentDiscovery.d.ts +3 -3
- package/workflows/container-contents/documentDiscovery.js +24 -96
- package/workflows/container-contents/documentDiscovery.js.map +1 -1
- package/workflows/container-contents/documentDiscoveryApply.js +65 -0
- package/workflows/container-contents/documentDiscoveryApply.js.map +1 -0
- package/workflows/container-contents/documentDiscoveryLanes.js +53 -0
- package/workflows/container-contents/documentDiscoveryLanes.js.map +1 -0
- package/workflows/container-contents/documentDiscoveryTypes.d.ts +25 -19
- package/workflows/container-contents/documentDiscoveryTypes.js.map +1 -1
- package/workflows/container-contents/documentPriming.js +2 -1
- package/workflows/container-contents/documentPriming.js.map +1 -1
- package/workflows/container-contents/documentQueries/sql.js +4 -4
- package/workflows/container-contents/documentQueries/sql.js.map +1 -1
- package/workflows/container-contents/pendingWrites/deferredTails.js +6 -3
- package/workflows/container-contents/pendingWrites/deferredTails.js.map +1 -1
- package/workflows/container-contents/pendingWrites/sources.js +2 -1
- package/workflows/container-contents/pendingWrites/sources.js.map +1 -1
- package/workflows/container-contents/pendingWrites.js +2 -1
- package/workflows/container-contents/pendingWrites.js.map +1 -1
- package/workflows/container-contents/remoteHydration/laneFetchMarkers.js +11 -17
- package/workflows/container-contents/remoteHydration/laneFetchMarkers.js.map +1 -1
- package/workflows/containers/child/create.js +3 -3
- package/workflows/containers/child/create.js.map +1 -1
- package/workflows/containers/child/move.js +2 -2
- package/workflows/containers/child/move.js.map +1 -1
- package/workflows/containers/child/mutationRequestCore.js +5 -4
- package/workflows/containers/child/mutationRequestCore.js.map +1 -1
- package/workflows/containers/child/recitePlan.js +3 -2
- package/workflows/containers/child/recitePlan.js.map +1 -1
- package/workflows/containers/child/sharePlanCore.js +2 -2
- package/workflows/containers/child/sharePlanCore.js.map +1 -1
- package/workflows/containers/root/create.js +2 -2
- package/workflows/containers/root/create.js.map +1 -1
- package/workflows/registration/provisioningBootstrapBuilders.d.ts +20 -46
- package/workflows/sync/remoteReset.js +9 -9
- package/workflows/sync/remoteReset.js.map +1 -1
- package/workflows/sync/remoteResetCursorScope.js +10 -18
- package/workflows/sync/remoteResetCursorScope.js.map +1 -1
- package/workflows/sync/remoteResetPlans.js +6 -3
- package/workflows/sync/remoteResetPlans.js.map +1 -1
- package/workflows/sync/remoteResetScope.js +4 -11
- package/workflows/sync/remoteResetScope.js.map +1 -1
- package/internal/validators/operation/blobTransferSchemas.js +0 -11
- package/internal/validators/operation/blobTransferSchemas.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","sourceRoot":"","sources":["service.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,GAEjC,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,qCAAqC,EAErC,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EACL,4BAA4B,EAC5B,gCAAgC,EAChC,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAA0B,MAAM,SAAS,CAAC;AAOvE,SAAS,YAAY,CAAC,MAAmC;IACvD,OAAO,MAAM,CAAC,QAAQ,KAAK,OAAO,IAAI,MAAM,CAAC,eAAe,IAAI,MAAM,CAAC,MAAM,CAAC;AAChF,CAAC;AAgBD,KAAK,UAAU,gBAAgB,CAC7B,IAAwB,EACxB,KAA0B;IAE1B,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,EAAE,CAAC;QAC5D,OAAO;IACT,CAAC;IACD,MAAM,mBAAmB,GAAG,KAAK,CAAC,mBAAmB,CAAC;IAEtD,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IAC1C,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,KAAK,CAAC,oBAAoB,CAAC,GAAG,EAAE,CAAC;QACvC,yBAAyB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACvC,OAAO;IACT,CAAC;IACD,wEAAwE;IACxE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gB;QAChB,mBAAmB;QACnB,eAAe,EAAE,GAAG,EAAE;YACpB,KAAK,CAAC,sBAAsB,CAAC,KAAK,EAAE,CAAC;YACrC,KAAK,CAAC,oBAAoB,CAAC,oBAAoB,EAAE,CAAC;QACpD,CAAC;QACD,0BAA0B,EAAE,CAAC,OAAO,EAAE,EAAE,CACtC,uBAAuB,CAAC;YACtB,YAAY,EAAE,OAAO,EAAE,YAAY,KAAK,IAAI;YAC5C,IAAI;YACJ,KAAK;YACL,WAAW,EAAE,IAAI,CAAC,eAAe;YACjC,gBAAgB,EAAE,IAAI,CAAC,oCAAoC;YAC3D,KAAK,EAAE,MAAM;SACd,CAAC;QACJ,YAAY,EAAE,GAAG,EAAE,CACjB,uBAAuB,CAAC;YACtB,YAAY,EAAE,KAAK;YACnB,IAAI;YACJ,KAAK;YACL,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,gBAAgB,EAAE,GAAG,EAAE,CAAC,2BAA2B,CAAC,IAAI,EAAE,KAAK,CAAC;YAChE,KAAK,EAAE,MAAM;SACd,CAAC;QACJ,IAAI,EAAE,GAAG,EAAE,CAAC,yBAAyB,CAAC,IAAI,EAAE,KAAK,CAAC;KACnD,CAAC;AACJ,CAAC","sourcesContent":["import {\n getOrCreateDomainSyncCoordinator,\n type SyncLane,\n} from \"../../data/sync/syncCoordinator\";\nimport {\n acknowledgeContainerForce,\n activateContainer,\n enqueueKnownContainersForIdleBackfill,\n type IdleBackfillState,\n markContainerForced,\n} from \"./idleBackfill\";\nimport { createInitialDocumentProbe } from \"./initialDocumentProbe\";\nimport {\n clearPendingDiscoveryRetries,\n isCurrentReconciliationLifecycle,\n reconcileMarkedContainer,\n sweepKnownContainers,\n} from \"./knownContainerSweep\";\nimport { rearmFailedContainer } from \"./laneFailure\";\nimport { clearOriginatedDocuments } from \"./originatedDocuments\";\nimport { createReconcileQueue, type ReconcilePriority } from \"./queue\";\nimport type {\n ReconciliationHost,\n ReconciliationRuntimeStatus,\n ReconciliationService,\n} from \"./serviceTypes\";\n\nfunction canReconcile(status: ReconciliationRuntimeStatus): boolean {\n return status.dbStatus === \"ready\" && status.isAuthenticated && status.online;\n}\n\ntype RefreshScope = \"root\" | \"full\";\n\ninterface ReconciliationState extends IdleBackfillState {\n active: boolean;\n lane: SyncLane | null;\n lifecycleGeneration: number;\n probeContinuationCancel: (() => void) | null;\n pendingRefresh: {\n readonly promise: Promise<void>;\n readonly scope: RefreshScope;\n readonly started: { value: boolean };\n } | null;\n}\n\nasync function runReconcileLane(\n host: ReconciliationHost,\n state: ReconciliationState,\n): Promise<void> {\n if (!state.active || !canReconcile(host.getRuntimeStatus())) {\n return;\n }\n const lifecycleGeneration = state.lifecycleGeneration;\n\n const containerId = state.queue.dequeue();\n if (!containerId) {\n await state.initialDocumentProbe.run();\n scheduleProbeContinuation(host, state);\n return;\n }\n // Double-check at run time: a container may have been discovered (by an\n // explicit refresh or an earlier lane pass) after it was queued. Mark it\n // discovered up front to collapse concurrent re-enqueues into one fetch, but\n // roll the mark back on failure so a transient error can be retried later.\n const forceGeneration = state.forcedContainerGenerations.get(containerId);\n const shouldForce = forceGeneration !== undefined;\n if (shouldForce || !state.discoveredContainerIds.has(containerId)) {\n state.discoveredContainerIds.add(containerId);\n try {\n const reconciled = await reconcileMarkedContainer(\n host,\n state,\n containerId,\n shouldForce,\n );\n if (reconciled) {\n acknowledgeContainerForce(state, containerId, forceGeneration);\n }\n } catch (error) {\n rearmFailedContainer(\n state,\n containerId,\n forceGeneration,\n lifecycleGeneration,\n );\n throw error;\n }\n }\n\n // Keep draining: schedule another pass while the queue holds work.\n if (state.queue.size > 0) {\n state.lane?.requestSync();\n } else {\n scheduleProbeContinuation(host, state);\n }\n}\n\nfunction scheduleProbeContinuation(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n state.probeContinuationCancel?.();\n const canContinue = () =>\n state.active &&\n state.queue.size === 0 &&\n canReconcile(host.getRuntimeStatus()) &&\n state.initialDocumentProbe.hasPendingWork();\n if (!canContinue()) {\n state.probeContinuationCancel = null;\n return;\n }\n const timeout = setTimeout(() => {\n if (canContinue()) {\n state.probeContinuationCancel = null;\n state.lane?.requestSync();\n }\n }, state.initialDocumentProbe.continuationDelayMs());\n state.probeContinuationCancel = () => clearTimeout(timeout);\n}\n\nfunction forgetIneligibleDiscoveredContainers(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n for (const containerId of state.discoveredContainerIds) {\n if (!host.canDiscoverContainerDocuments(containerId)) {\n state.discoveredContainerIds.delete(containerId);\n }\n }\n}\n\nasync function refreshAndReconcileContainers(input: {\n forceAllDocumentContentPulls: boolean;\n host: ReconciliationHost;\n listContainerIds: () => ReadonlyArray<string>;\n refreshTree: () => Promise<void>;\n state: ReconciliationState;\n}): Promise<void> {\n const { host, listContainerIds, refreshTree, state } = input;\n if (!canReconcile(host.getRuntimeStatus())) {\n return;\n }\n const lifecycleGeneration = state.lifecycleGeneration;\n\n if (input.forceAllDocumentContentPulls) {\n state.queue.clear();\n }\n try {\n // A resync_required structural refresh runs independently of this service.\n // If it already removed a container after the forced document lane won its\n // race against the old tree, forget the stale discovered bit before this\n // refresh can re-add the same id.\n forgetIneligibleDiscoveredContainers(host, state);\n await refreshTree();\n if (!isCurrentReconciliationLifecycle(state, lifecycleGeneration)) {\n return;\n }\n // Structural hydration can revoke and remove a container after a targeted\n // force already reconciled it against the old tree. Forget every id that\n // is no longer remotely listable so a later share of the same container id\n // is treated as newly surfaced instead of being suppressed for the rest of\n // the session.\n forgetIneligibleDiscoveredContainers(host, state);\n await sweepKnownContainers({\n forceAllDocumentContentPulls: input.forceAllDocumentContentPulls,\n host,\n knownIds: listContainerIds(),\n lifecycleGeneration,\n state,\n });\n if (!isCurrentReconciliationLifecycle(state, lifecycleGeneration)) {\n return;\n }\n } catch (error) {\n if (!isCurrentReconciliationLifecycle(state, lifecycleGeneration)) {\n return;\n }\n if (!host.isIgnorableError(error)) {\n throw error;\n }\n }\n}\n\n// Sweeps share the queue and discovered set, so they must run serially.\n// Root requests join either kind of sweep; a full request after a root sweep\n// must wait for it and then refresh the whole tree. A fresh-listing request\n// joins only a sweep that has not started: one already running may have listed\n// before the event behind the request, so it queues a trailing sweep instead.\nfunction requestContainerRefresh(input: {\n freshListing: boolean;\n host: ReconciliationHost;\n state: ReconciliationState;\n refreshTree: () => Promise<void>;\n listContainerIds: () => ReadonlyArray<string>;\n scope: RefreshScope;\n}): Promise<void> {\n const { state, scope } = input;\n const pending = state.pendingRefresh;\n if (\n pending &&\n (scope === \"root\" || pending.scope === \"full\") &&\n !(input.freshListing && pending.started.value)\n ) {\n return pending.promise;\n }\n\n const started = { value: false };\n const promise = (pending?.promise ?? Promise.resolve())\n // A failed earlier sweep must not prevent the queued refresh.\n .catch(() => undefined)\n .then(() => {\n started.value = true;\n return refreshAndReconcileContainers({\n forceAllDocumentContentPulls: scope === \"full\",\n host: input.host,\n listContainerIds: input.listContainerIds,\n refreshTree: input.refreshTree,\n state,\n });\n })\n .finally(() => {\n if (state.pendingRefresh?.promise === promise) {\n state.pendingRefresh = null;\n }\n });\n state.pendingRefresh = { promise, scope, started };\n return promise;\n}\n\nfunction listFullRefreshContainerIds(\n host: ReconciliationHost,\n state: ReconciliationState,\n): ReadonlyArray<string> {\n const knownIds = host.listKnownContainerIds();\n const activeContainerId = state.activeContainerId;\n if (\n !activeContainerId ||\n knownIds.includes(activeContainerId) ||\n !host.canDiscoverContainerDocuments(activeContainerId)\n ) {\n return knownIds;\n }\n // Explicit refresh also retries an open foreign system container that the\n // background discovery list excludes.\n return [...knownIds, activeContainerId];\n}\n\nfunction startReconciliationLane(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n if (state.active) {\n return;\n }\n state.lifecycleGeneration += 1;\n state.active = true;\n state.lane = getOrCreateDomainSyncCoordinator(host.domainScope).registerLane(\n \"reconciliation:documents\",\n {\n label: \"Device-first document reconciliation\",\n // Document discovery runs in the document phase, after structural\n // container hydration/metadata sync settles for the scope.\n phase: \"document\",\n priority: () => (state.activeContainerId ? -1 : 0),\n onUnexpectedError: (error) => {\n console.error(\"Device-first reconciliation failed:\", error);\n },\n // Fixed literal: no container or document id may reach a report. The\n // mapped stack identifies which lane body threw. Returning the host's\n // result hands a rejection to the lane reporter's wrapper; discarding it\n // would surface as an unhandled rejection instead.\n reportUnexpectedError: (error) =>\n host.logError?.(\"Reconciliation: sync lane failed\", error),\n run: () => runReconcileLane(host, state),\n shouldIgnoreError: host.isIgnorableError,\n },\n );\n}\n\nfunction createReconciliationState(\n host: ReconciliationHost,\n): ReconciliationState {\n return {\n active: false,\n activeContainerId: null,\n automaticRetryGenerations: new Map(),\n discoveredContainerIds: new Set(),\n forcedContainerGenerations: new Map(),\n forcedDocumentIds: new Map(),\n initialDocumentProbe: createInitialDocumentProbe(host),\n lane: null,\n lifecycleGeneration: 0,\n nextForceGeneration: 0,\n probeContinuationCancel: null,\n queue: createReconcileQueue(),\n pendingRefresh: null,\n };\n}\n\nfunction stopReconciliationService(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n clearPendingDiscoveryRetries(state);\n state.active = false;\n state.probeContinuationCancel?.();\n state.probeContinuationCancel = null;\n state.queue.clear();\n state.automaticRetryGenerations.clear();\n state.forcedContainerGenerations.clear();\n state.forcedDocumentIds.clear();\n state.pendingRefresh = null;\n state.initialDocumentProbe.resetPending();\n // Drop the per-session discovered suppression cache too: a stopped\n // reconciler is being torn down (scope/identity change) or paused across\n // a prerequisite loss, after which every container must be re-validated.\n state.discoveredContainerIds.clear();\n // Drop pending self-echo originations too — they are session-scoped and a\n // teardown invalidates them.\n clearOriginatedDocuments(host.domainScope);\n}\n\nexport function createReconciliationService(\n host: ReconciliationHost,\n): ReconciliationService {\n const state = createReconciliationState(host);\n\n const scheduleDrain = () => {\n if (\n !state.active ||\n (state.queue.size === 0 && !state.initialDocumentProbe.canRun())\n ) {\n return;\n }\n if (!canReconcile(host.getRuntimeStatus())) {\n return;\n }\n state.probeContinuationCancel?.();\n state.probeContinuationCancel = null;\n state.lane?.requestSync();\n };\n\n const enqueueContainer = (\n containerId: string,\n priority: ReconcilePriority,\n force = false,\n documentId?: string,\n ) => {\n if (!containerId) {\n return;\n }\n // Skip containers already reconciled this session unless forced. Events\n // force re-discovery; passive active/backfill scheduling does not, so\n // opening Explorer does not re-fetch every container on each navigation.\n if (\n !force &&\n !state.forcedContainerGenerations.has(containerId) &&\n state.discoveredContainerIds.has(containerId)\n ) {\n return;\n }\n if (force) {\n markContainerForced(state, containerId, documentId);\n }\n state.queue.enqueue(containerId, priority);\n scheduleDrain();\n };\n\n const enqueueIdleBackfill = () => {\n enqueueKnownContainersForIdleBackfill({\n host,\n scheduleDrain,\n state,\n });\n };\n\n return {\n start: () => {\n startReconciliationLane(host, state);\n scheduleDrain();\n },\n // Reconcile the active container first; siblings remain lazy until visited.\n setActiveContainer: (containerId) =>\n activateContainer(state, containerId, (activeId) =>\n enqueueContainer(activeId, \"active\"),\n ),\n enqueueContainer,\n enqueueIdleBackfill,\n resetDiscovered: () => {\n state.discoveredContainerIds.clear();\n state.initialDocumentProbe.resetSkippedListings();\n },\n reconcileRootContainersNow: (options) =>\n requestContainerRefresh({\n freshListing: options?.freshListing === true,\n host,\n state,\n refreshTree: host.refreshRootTree,\n listContainerIds: host.listAutomaticRootCatchupContainerIds,\n scope: \"root\",\n }),\n reconcileNow: () =>\n requestContainerRefresh({\n freshListing: false,\n host,\n state,\n refreshTree: host.refreshTree,\n listContainerIds: () => listFullRefreshContainerIds(host, state),\n scope: \"full\",\n }),\n stop: () => stopReconciliationService(host, state),\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["service.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,GAEjC,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,qCAAqC,EAErC,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,C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CAAC,IAAI,EAAE,KAAK,CAAC;YAChE,KAAK,EAAE,MAAM;SACd,CAAC;QACJ,IAAI,EAAE,GAAG,EAAE,CAAC,yBAAyB,CAAC,IAAI,EAAE,KAAK,CAAC;KACnD,CAAC;AACJ,CAAC","sourcesContent":["import {\n getOrCreateDomainSyncCoordinator,\n type SyncLane,\n} from \"../../data/sync/syncCoordinator\";\nimport {\n acknowledgeContainerForce,\n activateContainer,\n enqueueKnownContainersForIdleBackfill,\n type IdleBackfillState,\n markContainerForced,\n} from \"./idleBackfill\";\nimport { createInitialDocumentProbe } from \"./initialDocumentProbe\";\nimport {\n clearPendingDiscoveryRetries,\n isCurrentReconciliationLifecycle,\n reconcileMarkedContainers,\n sweepKnownContainers,\n} from \"./knownContainerSweep\";\nimport { rearmFailedContainer } from \"./laneFailure\";\nimport { clearOriginatedDocuments } from \"./originatedDocuments\";\nimport { createReconcileQueue, type ReconcilePriority } from \"./queue\";\nimport type {\n ReconciliationHost,\n ReconciliationRuntimeStatus,\n ReconciliationService,\n} from \"./serviceTypes\";\n\nfunction canReconcile(status: ReconciliationRuntimeStatus): boolean {\n return status.dbStatus === \"ready\" && status.isAuthenticated && status.online;\n}\n\ntype RefreshScope = \"root\" | \"full\";\n\ninterface ReconciliationState extends IdleBackfillState {\n active: boolean;\n lane: SyncLane | null;\n lifecycleGeneration: number;\n probeContinuationCancel: (() => void) | null;\n pendingRefresh: {\n readonly promise: Promise<void>;\n readonly scope: RefreshScope;\n readonly started: { value: boolean };\n } | null;\n}\n\n/**\n * Queued containers one lane run lists together. Each still verifies and\n * applies on its own; the bound keeps one run's head verification well\n * inside the lane watchdog.\n */\nconst RECONCILE_LANE_BATCH_SIZE = 4;\n\nfunction dequeueContainerBatch(state: ReconciliationState): string[] {\n const containerIds: string[] = [];\n while (containerIds.length < RECONCILE_LANE_BATCH_SIZE) {\n const containerId = state.queue.dequeue();\n if (!containerId) break;\n containerIds.push(containerId);\n }\n return containerIds;\n}\n\nasync function runReconcileLane(\n host: ReconciliationHost,\n state: ReconciliationState,\n): Promise<void> {\n if (!state.active || !canReconcile(host.getRuntimeStatus())) {\n return;\n }\n const lifecycleGeneration = state.lifecycleGeneration;\n\n const containerIds = dequeueContainerBatch(state);\n if (containerIds.length === 0) {\n await state.initialDocumentProbe.run();\n scheduleProbeContinuation(host, state);\n return;\n }\n // Double-check at run time: a container may have been discovered (by an\n // explicit refresh or an earlier lane pass) after it was queued. Mark it\n // discovered up front to collapse concurrent re-enqueues into one fetch, but\n // roll the mark back on failure so a transient error can be retried. Force\n // generations are read before the batched listing starts.\n const batch = containerIds.flatMap((containerId) => {\n const forceGeneration = state.forcedContainerGenerations.get(containerId);\n if (\n forceGeneration === undefined &&\n state.discoveredContainerIds.has(containerId)\n ) {\n return [];\n }\n state.discoveredContainerIds.add(containerId);\n return [{ containerId, forceGeneration }];\n });\n const results = await reconcileMarkedContainers(\n host,\n state,\n batch.map(({ containerId, forceGeneration }) => ({\n containerId,\n forceDocumentContentPull: forceGeneration !== undefined,\n })),\n );\n let firstError: unknown;\n results.forEach((result, index) => {\n const forceGeneration = batch[index]?.forceGeneration;\n if (result.status === \"reconciled\") {\n acknowledgeContainerForce(state, result.containerId, forceGeneration);\n } else if (result.status === \"failed\") {\n rearmFailedContainer(\n state,\n result.containerId,\n forceGeneration,\n lifecycleGeneration,\n );\n firstError ??= result.error;\n }\n });\n if (firstError !== undefined) throw firstError;\n\n // Keep draining: schedule another pass while the queue holds work.\n if (state.queue.size > 0) {\n state.lane?.requestSync();\n } else {\n scheduleProbeContinuation(host, state);\n }\n}\n\nfunction scheduleProbeContinuation(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n state.probeContinuationCancel?.();\n const canContinue = () =>\n state.active &&\n state.queue.size === 0 &&\n canReconcile(host.getRuntimeStatus()) &&\n state.initialDocumentProbe.hasPendingWork();\n if (!canContinue()) {\n state.probeContinuationCancel = null;\n return;\n }\n const timeout = setTimeout(() => {\n if (canContinue()) {\n state.probeContinuationCancel = null;\n state.lane?.requestSync();\n }\n }, state.initialDocumentProbe.continuationDelayMs());\n state.probeContinuationCancel = () => clearTimeout(timeout);\n}\n\nfunction forgetIneligibleDiscoveredContainers(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n for (const containerId of state.discoveredContainerIds) {\n if (!host.canDiscoverContainerDocuments(containerId)) {\n state.discoveredContainerIds.delete(containerId);\n }\n }\n}\n\nasync function refreshAndReconcileContainers(input: {\n forceAllDocumentContentPulls: boolean;\n host: ReconciliationHost;\n listContainerIds: () => ReadonlyArray<string>;\n refreshTree: () => Promise<void>;\n state: ReconciliationState;\n}): Promise<void> {\n const { host, listContainerIds, refreshTree, state } = input;\n if (!canReconcile(host.getRuntimeStatus())) {\n return;\n }\n const lifecycleGeneration = state.lifecycleGeneration;\n\n if (input.forceAllDocumentContentPulls) {\n state.queue.clear();\n }\n try {\n // A resync_required structural refresh runs independently of this service.\n // If it already removed a container after the forced document lane won its\n // race against the old tree, forget the stale discovered bit before this\n // refresh can re-add the same id.\n forgetIneligibleDiscoveredContainers(host, state);\n await refreshTree();\n if (!isCurrentReconciliationLifecycle(state, lifecycleGeneration)) {\n return;\n }\n // Structural hydration can revoke and remove a container after a targeted\n // force already reconciled it against the old tree. Forget every id that\n // is no longer remotely listable so a later share of the same container id\n // is treated as newly surfaced instead of being suppressed for the rest of\n // the session.\n forgetIneligibleDiscoveredContainers(host, state);\n await sweepKnownContainers({\n forceAllDocumentContentPulls: input.forceAllDocumentContentPulls,\n host,\n knownIds: listContainerIds(),\n lifecycleGeneration,\n state,\n });\n if (!isCurrentReconciliationLifecycle(state, lifecycleGeneration)) {\n return;\n }\n } catch (error) {\n if (!isCurrentReconciliationLifecycle(state, lifecycleGeneration)) {\n return;\n }\n if (!host.isIgnorableError(error)) {\n throw error;\n }\n }\n}\n\n// Sweeps share the queue and discovered set, so they must run serially.\n// Root requests join either kind of sweep; a full request after a root sweep\n// must wait for it and then refresh the whole tree. A fresh-listing request\n// joins only a sweep that has not started: one already running may have listed\n// before the event behind the request, so it queues a trailing sweep instead.\nfunction requestContainerRefresh(input: {\n freshListing: boolean;\n host: ReconciliationHost;\n state: ReconciliationState;\n refreshTree: () => Promise<void>;\n listContainerIds: () => ReadonlyArray<string>;\n scope: RefreshScope;\n}): Promise<void> {\n const { state, scope } = input;\n const pending = state.pendingRefresh;\n if (\n pending &&\n (scope === \"root\" || pending.scope === \"full\") &&\n !(input.freshListing && pending.started.value)\n ) {\n return pending.promise;\n }\n\n const started = { value: false };\n const promise = (pending?.promise ?? Promise.resolve())\n // A failed earlier sweep must not prevent the queued refresh.\n .catch(() => undefined)\n .then(() => {\n started.value = true;\n return refreshAndReconcileContainers({\n forceAllDocumentContentPulls: scope === \"full\",\n host: input.host,\n listContainerIds: input.listContainerIds,\n refreshTree: input.refreshTree,\n state,\n });\n })\n .finally(() => {\n if (state.pendingRefresh?.promise === promise) {\n state.pendingRefresh = null;\n }\n });\n state.pendingRefresh = { promise, scope, started };\n return promise;\n}\n\nfunction listFullRefreshContainerIds(\n host: ReconciliationHost,\n state: ReconciliationState,\n): ReadonlyArray<string> {\n const knownIds = host.listKnownContainerIds();\n const activeContainerId = state.activeContainerId;\n if (\n !activeContainerId ||\n knownIds.includes(activeContainerId) ||\n !host.canDiscoverContainerDocuments(activeContainerId)\n ) {\n return knownIds;\n }\n // Explicit refresh also retries an open foreign system container that the\n // background discovery list excludes.\n return [...knownIds, activeContainerId];\n}\n\nfunction startReconciliationLane(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n if (state.active) {\n return;\n }\n state.lifecycleGeneration += 1;\n state.active = true;\n state.lane = getOrCreateDomainSyncCoordinator(host.domainScope).registerLane(\n \"reconciliation:documents\",\n {\n label: \"Device-first document reconciliation\",\n // Document discovery runs in the document phase, after structural\n // container hydration/metadata sync settles for the scope.\n phase: \"document\",\n priority: () => (state.activeContainerId ? -1 : 0),\n onUnexpectedError: (error) => {\n console.error(\"Device-first reconciliation failed:\", error);\n },\n // Fixed literal: no container or document id may reach a report. The\n // mapped stack identifies which lane body threw. Returning the host's\n // result hands a rejection to the lane reporter's wrapper; discarding it\n // would surface as an unhandled rejection instead.\n reportUnexpectedError: (error) =>\n host.logError?.(\"Reconciliation: sync lane failed\", error),\n run: () => runReconcileLane(host, state),\n shouldIgnoreError: host.isIgnorableError,\n },\n );\n}\n\nfunction createReconciliationState(\n host: ReconciliationHost,\n): ReconciliationState {\n return {\n active: false,\n activeContainerId: null,\n automaticRetryGenerations: new Map(),\n discoveredContainerIds: new Set(),\n forcedContainerGenerations: new Map(),\n forcedDocumentIds: new Map(),\n initialDocumentProbe: createInitialDocumentProbe(host),\n lane: null,\n lifecycleGeneration: 0,\n nextForceGeneration: 0,\n probeContinuationCancel: null,\n queue: createReconcileQueue(),\n pendingRefresh: null,\n };\n}\n\nfunction stopReconciliationService(\n host: ReconciliationHost,\n state: ReconciliationState,\n): void {\n clearPendingDiscoveryRetries(state);\n state.active = false;\n state.probeContinuationCancel?.();\n state.probeContinuationCancel = null;\n state.queue.clear();\n state.automaticRetryGenerations.clear();\n state.forcedContainerGenerations.clear();\n state.forcedDocumentIds.clear();\n state.pendingRefresh = null;\n state.initialDocumentProbe.resetPending();\n // Drop the per-session discovered suppression cache too: a stopped\n // reconciler is being torn down (scope/identity change) or paused across\n // a prerequisite loss, after which every container must be re-validated.\n state.discoveredContainerIds.clear();\n // Drop pending self-echo originations too — they are session-scoped and a\n // teardown invalidates them.\n clearOriginatedDocuments(host.domainScope);\n}\n\nexport function createReconciliationService(\n host: ReconciliationHost,\n): ReconciliationService {\n const state = createReconciliationState(host);\n\n const scheduleDrain = () => {\n if (\n !state.active ||\n (state.queue.size === 0 && !state.initialDocumentProbe.canRun())\n ) {\n return;\n }\n if (!canReconcile(host.getRuntimeStatus())) {\n return;\n }\n state.probeContinuationCancel?.();\n state.probeContinuationCancel = null;\n state.lane?.requestSync();\n };\n\n const enqueueContainer = (\n containerId: string,\n priority: ReconcilePriority,\n force = false,\n documentId?: string,\n ) => {\n if (!containerId) {\n return;\n }\n // Skip containers already reconciled this session unless forced. Events\n // force re-discovery; passive active/backfill scheduling does not, so\n // opening Explorer does not re-fetch every container on each navigation.\n if (\n !force &&\n !state.forcedContainerGenerations.has(containerId) &&\n state.discoveredContainerIds.has(containerId)\n ) {\n return;\n }\n if (force) {\n markContainerForced(state, containerId, documentId);\n }\n state.queue.enqueue(containerId, priority);\n scheduleDrain();\n };\n\n const enqueueIdleBackfill = () => {\n enqueueKnownContainersForIdleBackfill({\n host,\n scheduleDrain,\n state,\n });\n };\n\n return {\n start: () => {\n startReconciliationLane(host, state);\n scheduleDrain();\n },\n // Reconcile the active container first; siblings remain lazy until visited.\n setActiveContainer: (containerId) =>\n activateContainer(state, containerId, (activeId) =>\n enqueueContainer(activeId, \"active\"),\n ),\n enqueueContainer,\n enqueueIdleBackfill,\n resetDiscovered: () => {\n state.discoveredContainerIds.clear();\n state.initialDocumentProbe.resetSkippedListings();\n },\n reconcileRootContainersNow: (options) =>\n requestContainerRefresh({\n freshListing: options?.freshListing === true,\n host,\n state,\n refreshTree: host.refreshRootTree,\n listContainerIds: host.listAutomaticRootCatchupContainerIds,\n scope: \"root\",\n }),\n reconcileNow: () =>\n requestContainerRefresh({\n freshListing: false,\n host,\n state,\n refreshTree: host.refreshTree,\n listContainerIds: () => listFullRefreshContainerIds(host, state),\n scope: \"full\",\n }),\n stop: () => stopReconciliationService(host, state),\n };\n}\n"]}
|
|
@@ -3,6 +3,22 @@ import type { DomainScope } from "../../data/domainScope.js";
|
|
|
3
3
|
import type { LocalProjectionReconciledDelta } from "../../stores/local-projection/index.js";
|
|
4
4
|
import type { InitialDocumentProbeHost } from "./initialDocumentProbe.js";
|
|
5
5
|
import type { ReconcilePriority } from "./queue.js";
|
|
6
|
+
/** One container a reconciliation pass lists, with its per-lane callbacks. */
|
|
7
|
+
export interface ContainerDiscoveryRequest {
|
|
8
|
+
readonly containerId: string;
|
|
9
|
+
readonly onFullListing?: ((documentIds: ReadonlyArray<string>) => void) | undefined;
|
|
10
|
+
readonly onPendingDiscovery?: ((delayMs: number) => void) | undefined;
|
|
11
|
+
}
|
|
12
|
+
/** A discovered lane: `null` means it was not fully listed or applied nothing. */
|
|
13
|
+
export type ContainerDiscoveryOutcome = {
|
|
14
|
+
readonly containerId: string;
|
|
15
|
+
readonly status: "fulfilled";
|
|
16
|
+
readonly value: ReadonlyArray<DocumentSummary> | null;
|
|
17
|
+
} | {
|
|
18
|
+
readonly containerId: string;
|
|
19
|
+
readonly reason: unknown;
|
|
20
|
+
readonly status: "rejected";
|
|
21
|
+
};
|
|
6
22
|
export interface ReconciliationRuntimeStatus {
|
|
7
23
|
dbStatus: string;
|
|
8
24
|
isAuthenticated: boolean;
|
|
@@ -22,8 +38,13 @@ export interface ReconciliationHost extends InitialDocumentProbeHost {
|
|
|
22
38
|
listKnownContainerIds: () => ReadonlyArray<string>;
|
|
23
39
|
/** Known ids eligible after an automatic root-lane discovery hint. */
|
|
24
40
|
listAutomaticRootCatchupContainerIds: () => ReadonlyArray<string>;
|
|
25
|
-
/**
|
|
26
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Discover + persist documents for every requested container through one
|
|
43
|
+
* batched listing. Each lane's outcome is handed to `onOutcome` (awaited)
|
|
44
|
+
* as soon as it applies, before later lanes apply; every outcome is also
|
|
45
|
+
* returned.
|
|
46
|
+
*/
|
|
47
|
+
discoverContainerDocuments: (requests: ReadonlyArray<ContainerDiscoveryRequest>, onOutcome: (outcome: ContainerDiscoveryOutcome) => Promise<void>) => Promise<ReadonlyArray<ContainerDiscoveryOutcome>>;
|
|
27
48
|
/** Read a container's freshly-persisted summaries+links from SQLite. */
|
|
28
49
|
loadContainerDelta: (containerId: string) => Promise<LocalProjectionReconciledDelta>;
|
|
29
50
|
/** Push a reconciled delta into the local projection store. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceTypes.js","sourceRoot":"","sources":["serviceTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { DocumentSummary } from \"../../data/documents/documentSummary\";\nimport type { DomainScope } from \"../../data/domainScope\";\nimport type { LocalProjectionReconciledDelta } from \"../../stores/local-projection\";\nimport type { InitialDocumentProbeHost } from \"./initialDocumentProbe\";\nimport type { ReconcilePriority } from \"./queue\";\n\nexport interface ReconciliationRuntimeStatus {\n dbStatus: string;\n isAuthenticated: boolean;\n online: boolean;\n}\n\n/**\n * Host contract injected by the device-first facade. Keeps the reconciler\n * React-free and decoupled from runtime/persistence construction — it only\n * orchestrates *when* these run and routes their results into Layer A.\n */\nexport interface ReconciliationHost extends InitialDocumentProbeHost {\n domainScope: DomainScope;\n getRuntimeStatus: () => ReconciliationRuntimeStatus;\n /** True when this container has enough remote state to list documents. */\n canDiscoverContainerDocuments: (containerId: string) => boolean;\n /** Known container ids eligible for idle document reconciliation. */\n listKnownContainerIds: () => ReadonlyArray<string>;\n /** Known ids eligible after an automatic root-lane discovery hint. */\n listAutomaticRootCatchupContainerIds: () => ReadonlyArray<string>;\n
|
|
1
|
+
{"version":3,"file":"serviceTypes.js","sourceRoot":"","sources":["serviceTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { DocumentSummary } from \"../../data/documents/documentSummary\";\nimport type { DomainScope } from \"../../data/domainScope\";\nimport type { LocalProjectionReconciledDelta } from \"../../stores/local-projection\";\nimport type { InitialDocumentProbeHost } from \"./initialDocumentProbe\";\nimport type { ReconcilePriority } from \"./queue\";\n\n/** One container a reconciliation pass lists, with its per-lane callbacks. */\nexport interface ContainerDiscoveryRequest {\n readonly containerId: string;\n readonly onFullListing?:\n | ((documentIds: ReadonlyArray<string>) => void)\n | undefined;\n readonly onPendingDiscovery?: ((delayMs: number) => void) | undefined;\n}\n\n/** A discovered lane: `null` means it was not fully listed or applied nothing. */\nexport type ContainerDiscoveryOutcome =\n | {\n readonly containerId: string;\n readonly status: \"fulfilled\";\n readonly value: ReadonlyArray<DocumentSummary> | null;\n }\n | {\n readonly containerId: string;\n readonly reason: unknown;\n readonly status: \"rejected\";\n };\n\nexport interface ReconciliationRuntimeStatus {\n dbStatus: string;\n isAuthenticated: boolean;\n online: boolean;\n}\n\n/**\n * Host contract injected by the device-first facade. Keeps the reconciler\n * React-free and decoupled from runtime/persistence construction — it only\n * orchestrates *when* these run and routes their results into Layer A.\n */\nexport interface ReconciliationHost extends InitialDocumentProbeHost {\n domainScope: DomainScope;\n getRuntimeStatus: () => ReconciliationRuntimeStatus;\n /** True when this container has enough remote state to list documents. */\n canDiscoverContainerDocuments: (containerId: string) => boolean;\n /** Known container ids eligible for idle document reconciliation. */\n listKnownContainerIds: () => ReadonlyArray<string>;\n /** Known ids eligible after an automatic root-lane discovery hint. */\n listAutomaticRootCatchupContainerIds: () => ReadonlyArray<string>;\n /**\n * Discover + persist documents for every requested container through one\n * batched listing. Each lane's outcome is handed to `onOutcome` (awaited)\n * as soon as it applies, before later lanes apply; every outcome is also\n * returned.\n */\n discoverContainerDocuments: (\n requests: ReadonlyArray<ContainerDiscoveryRequest>,\n onOutcome: (outcome: ContainerDiscoveryOutcome) => Promise<void>,\n ) => Promise<ReadonlyArray<ContainerDiscoveryOutcome>>;\n /** Read a container's freshly-persisted summaries+links from SQLite. */\n loadContainerDelta: (\n containerId: string,\n ) => Promise<LocalProjectionReconciledDelta>;\n /** Push a reconciled delta into the local projection store. */\n applyReconciled: (delta: LocalProjectionReconciledDelta) => void;\n /**\n * Sync document bodies for a reconciled container. `force` (an explicit\n * refresh) revalidates registered ordinary documents and retries system\n * documents; otherwise only unopened system documents are synced, so a\n * background pass can materialize projection-owned content without eagerly\n * opening every ordinary document. A documentIds set limits forced pulls to\n * hint-named documents while still discovering new system content.\n */\n requestDocumentContentPull: (\n containerId: string,\n documents: ReadonlyArray<DocumentSummary>,\n force: boolean,\n documentIds?: ReadonlySet<string>,\n ) => void;\n /** Refresh the container tree from the server (explicit refresh). */\n refreshTree: () => Promise<void>;\n /** Refresh only the top-level root lane from the server. */\n refreshRootTree: () => Promise<void>;\n /** True if the destroyed-db error should be swallowed rather than surfaced. */\n isIgnorableError: (error: unknown) => boolean;\n /** Host diagnostics sink; bare runtimes do not structure errors. */\n logError?: ((message: string, error: unknown) => void) | undefined;\n}\n\nexport interface ReconciliationService {\n start: () => void;\n setActiveContainer: (containerId: string | null) => void;\n /** Force a listing; documentId limits content revalidation to that hint. */\n enqueueContainer: (\n containerId: string,\n priority: ReconcilePriority,\n force?: boolean,\n documentId?: string,\n ) => void;\n /** Queue known containers at idle priority. */\n enqueueIdleBackfill: () => void;\n /**\n * Forget which containers were reconciled this session so the next enqueue of\n * each re-validates against the server exactly once. Call on the\n * cannot-reconcile → can-reconcile edge (relogin/reconnect): the discovered\n * set is a per-session suppression cache, a previously-visited container may\n * have changed remotely, and initial-probe listings that exhausted their\n * retries need a fresh opportunity once the server is reachable again.\n */\n resetDiscovered: () => void;\n /**\n * Re-list root containers and reconcile the root catch-up set. By default a\n * request joins a sweep already in flight. With `freshListing`, the returned\n * sweep starts after this call: a discovery event (a root shared with or\n * created for this user) must not be satisfied by a sweep that may have\n * listed roots before the event committed.\n */\n reconcileRootContainersNow: (options?: {\n readonly freshListing?: boolean;\n }) => Promise<void>;\n reconcileNow: () => Promise<void>;\n stop: () => void;\n}\n"]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describeMultipartBlobPart } from "../../internal/api-client/index.js";
|
|
2
|
+
import { bytesToHex } from "../../internal/crypto/index.js";
|
|
3
|
+
import { MAX_INITIATE_PART_UPLOADS, } from "../../internal/validators/util/index.js";
|
|
4
|
+
import { acquireBlobTransferPermit, tryAcquireBlobTransferPermit, } from "../../data/blobs/transferPool.js";
|
|
5
|
+
/** Return the pool slots of first parts no upload consumed. */
|
|
6
|
+
export function releasePreparedParts(prepared) {
|
|
7
|
+
for (const part of prepared.values()) {
|
|
8
|
+
part.permit.release();
|
|
9
|
+
}
|
|
10
|
+
prepared.clear();
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One permit is awaited; more are taken only if free right now. Two uploads
|
|
14
|
+
* each waiting for a second slot while holding a first could otherwise
|
|
15
|
+
* deadlock the pool, since a held slot frees only when its part uploads.
|
|
16
|
+
*/
|
|
17
|
+
async function acquireFirstPartPermits(client, limit) {
|
|
18
|
+
const permits = [await acquireBlobTransferPermit(client)];
|
|
19
|
+
while (permits.length < limit) {
|
|
20
|
+
const permit = tryAcquireBlobTransferPermit(client);
|
|
21
|
+
if (!permit)
|
|
22
|
+
break;
|
|
23
|
+
permits.push(permit);
|
|
24
|
+
}
|
|
25
|
+
return permits;
|
|
26
|
+
}
|
|
27
|
+
export async function sha256BytesHex(bytes) {
|
|
28
|
+
return bytesToHex(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)));
|
|
29
|
+
}
|
|
30
|
+
export async function prepareFirstMultipartParts(client, encryption, concurrency) {
|
|
31
|
+
const permits = await acquireFirstPartPermits(client, Math.min(encryption.partCount, concurrency, MAX_INITIATE_PART_UPLOADS));
|
|
32
|
+
const descriptors = [];
|
|
33
|
+
const prepared = new Map();
|
|
34
|
+
try {
|
|
35
|
+
for (const [partIndex, permit] of permits.entries()) {
|
|
36
|
+
const bytes = await encryption.encryptPart(partIndex);
|
|
37
|
+
if (bytes.byteLength !== encryption.getPartByteLength(partIndex)) {
|
|
38
|
+
throw new Error(`Encrypted blob part ${partIndex + 1} byte length mismatch: expected ${encryption.getPartByteLength(partIndex).toLocaleString()}, received ${bytes.byteLength.toLocaleString()}.`);
|
|
39
|
+
}
|
|
40
|
+
const sha256 = await sha256BytesHex(bytes);
|
|
41
|
+
prepared.set(partIndex, { bytes, permit, sha256 });
|
|
42
|
+
descriptors.push(describeMultipartBlobPart(partIndex + 1, bytes));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
for (const permit of permits)
|
|
47
|
+
permit.release();
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
return { descriptors, prepared };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The initiation must return exactly one source per described part, for that
|
|
54
|
+
* part's number, length, and checksum, and every source of one stage must use
|
|
55
|
+
* the same transport.
|
|
56
|
+
*/
|
|
57
|
+
export function readIssuedPartSources(descriptors, stage) {
|
|
58
|
+
const issued = new Map();
|
|
59
|
+
const kinds = new Set(stage.partUploads.map((upload) => upload.source.kind));
|
|
60
|
+
const matches = stage.partUploads.length === descriptors.length &&
|
|
61
|
+
kinds.size === 1 &&
|
|
62
|
+
descriptors.every((descriptor, index) => {
|
|
63
|
+
const upload = stage.partUploads[index];
|
|
64
|
+
return (upload?.partNumber === descriptor.partNumber &&
|
|
65
|
+
upload.byteLength === descriptor.byteLength &&
|
|
66
|
+
upload.contentMd5 === descriptor.contentMd5);
|
|
67
|
+
});
|
|
68
|
+
if (!matches) {
|
|
69
|
+
throw new Error("Multipart blob stage initiation part uploads do not match its request.");
|
|
70
|
+
}
|
|
71
|
+
for (const upload of stage.partUploads) {
|
|
72
|
+
issued.set(upload.partNumber, upload.source);
|
|
73
|
+
}
|
|
74
|
+
return { apiTransport: kinds.has("api"), issued };
|
|
75
|
+
}
|
|
76
|
+
/** Use each issued source once; retries and later parts authorize anew. */
|
|
77
|
+
export function takePartSource(sources, partNumber) {
|
|
78
|
+
const issued = sources.issued.get(partNumber);
|
|
79
|
+
sources.issued.delete(partNumber);
|
|
80
|
+
return issued ?? (sources.apiTransport ? { kind: "api" } : undefined);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=multipartFirstParts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipartFirstParts.js","sourceRoot":"","sources":["multipartFirstParts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,EACL,yBAAyB,GAG1B,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACL,yBAAyB,EAEzB,4BAA4B,GAC7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CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE;YACtC,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;YACxC,OAAO,CACL,MAAM,EAAE,UAAU,KAAK,UAAU,CAAC,UAAU;gBAC5C,MAAM,CAAC,UAAU,KAAK,UAAU,CAAC,UAAU;gBAC3C,MAAM,CAAC,UAAU,KAAK,UAAU,CAAC,UAAU,CAC5C,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,wEAAwE,CACzE,CAAC;IACJ,CAAC;IACD,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;QACvC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,EAAE,YAAY,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;AACpD,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,cAAc,CAC5B,OAA6B,EAC7B,UAAkB;IAElB,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC9C,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAClC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AACxE,CAAC","sourcesContent":["import { describeMultipartBlobPart } from \"@tearleads/api-client\";\nimport { bytesToHex } from \"@tearleads/crypto\";\nimport type { InitiateMultipartBlobStageResponse } from \"@tearleads/validators/response\";\nimport {\n MAX_INITIATE_PART_UPLOADS,\n type MultipartBlobPartDescriptor,\n type MultipartBlobPartUploadSource,\n} from \"@tearleads/validators/util\";\nimport type { BlobBytes } from \"../../data/blobContracts\";\nimport {\n acquireBlobTransferPermit,\n type BlobTransferPermit,\n tryAcquireBlobTransferPermit,\n} from \"../../data/blobs/transferPool\";\nimport type { BlobEncryptionPlan } from \"../../data/documents/blob/shared/crypto\";\n\nexport interface PreparedMultipartPart {\n readonly bytes: BlobBytes;\n readonly sha256: string;\n}\n\n/**\n * A first part's bytes and the transfer-pool permit that pays for them: the\n * bytes exist before their upload can start, so they hold a slot of the\n * client's byte-I/O bound until the upload that sends them releases it.\n */\nexport interface PreparedFirstPart extends PreparedMultipartPart {\n readonly permit: BlobTransferPermit;\n}\n\n/**\n * The parts a new stage's upload starts with, encrypted once up front: their\n * descriptors ride on the stage initiation, and their bytes are handed to the\n * upload workers instead of being encrypted a second time.\n */\nexport interface MultipartFirstParts {\n readonly descriptors: readonly MultipartBlobPartDescriptor[];\n readonly prepared: Map<number, PreparedFirstPart>;\n}\n\n/** Return the pool slots of first parts no upload consumed. */\nexport function releasePreparedParts(\n prepared: Map<number, PreparedFirstPart>,\n): void {\n for (const part of prepared.values()) {\n part.permit.release();\n }\n prepared.clear();\n}\n\n/**\n * One permit is awaited; more are taken only if free right now. Two uploads\n * each waiting for a second slot while holding a first could otherwise\n * deadlock the pool, since a held slot frees only when its part uploads.\n */\nasync function acquireFirstPartPermits(\n client: object,\n limit: number,\n): Promise<BlobTransferPermit[]> {\n const permits = [await acquireBlobTransferPermit(client)];\n while (permits.length < limit) {\n const permit = tryAcquireBlobTransferPermit(client);\n if (!permit) break;\n permits.push(permit);\n }\n return permits;\n}\n\n/**\n * Where each part's bytes go. Sources the stage initiation issued are used\n * once; a store that cannot sign (`api`) needs no per-part authorization at\n * all, and a part with no source authorizes just before it is sent.\n */\nexport interface MultipartPartSources {\n readonly apiTransport: boolean;\n readonly issued: Map<number, MultipartBlobPartUploadSource>;\n}\n\nexport async function sha256BytesHex(bytes: BlobBytes): Promise<string> {\n return bytesToHex(\n new Uint8Array(await crypto.subtle.digest(\"SHA-256\", bytes)),\n );\n}\n\nexport async function prepareFirstMultipartParts(\n client: object,\n encryption: BlobEncryptionPlan,\n concurrency: number,\n): Promise<MultipartFirstParts> {\n const permits = await acquireFirstPartPermits(\n client,\n Math.min(encryption.partCount, concurrency, MAX_INITIATE_PART_UPLOADS),\n );\n const descriptors: MultipartBlobPartDescriptor[] = [];\n const prepared = new Map<number, PreparedFirstPart>();\n try {\n for (const [partIndex, permit] of permits.entries()) {\n const bytes = await encryption.encryptPart(partIndex);\n if (bytes.byteLength !== encryption.getPartByteLength(partIndex)) {\n throw new Error(\n `Encrypted blob part ${partIndex + 1} byte length mismatch: expected ${encryption.getPartByteLength(partIndex).toLocaleString()}, received ${bytes.byteLength.toLocaleString()}.`,\n );\n }\n const sha256 = await sha256BytesHex(bytes);\n prepared.set(partIndex, { bytes, permit, sha256 });\n descriptors.push(describeMultipartBlobPart(partIndex + 1, bytes));\n }\n } catch (error) {\n for (const permit of permits) permit.release();\n throw error;\n }\n return { descriptors, prepared };\n}\n\n/**\n * The initiation must return exactly one source per described part, for that\n * part's number, length, and checksum, and every source of one stage must use\n * the same transport.\n */\nexport function readIssuedPartSources(\n descriptors: readonly MultipartBlobPartDescriptor[],\n stage: Pick<InitiateMultipartBlobStageResponse, \"partUploads\">,\n): MultipartPartSources {\n const issued = new Map<number, MultipartBlobPartUploadSource>();\n const kinds = new Set(stage.partUploads.map((upload) => upload.source.kind));\n const matches =\n stage.partUploads.length === descriptors.length &&\n kinds.size === 1 &&\n descriptors.every((descriptor, index) => {\n const upload = stage.partUploads[index];\n return (\n upload?.partNumber === descriptor.partNumber &&\n upload.byteLength === descriptor.byteLength &&\n upload.contentMd5 === descriptor.contentMd5\n );\n });\n if (!matches) {\n throw new Error(\n \"Multipart blob stage initiation part uploads do not match its request.\",\n );\n }\n for (const upload of stage.partUploads) {\n issued.set(upload.partNumber, upload.source);\n }\n return { apiTransport: kinds.has(\"api\"), issued };\n}\n\n/** Use each issued source once; retries and later parts authorize anew. */\nexport function takePartSource(\n sources: MultipartPartSources,\n partNumber: number,\n): MultipartBlobPartUploadSource | undefined {\n const issued = sources.issued.get(partNumber);\n sources.issued.delete(partNumber);\n return issued ?? (sources.apiTransport ? { kind: \"api\" } : undefined);\n}\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { prepareFirstMultipartParts, readIssuedPartSources, releasePreparedParts, } from "./multipartFirstParts.js";
|
|
2
|
+
import { assertMultipartStageReplaceable, multipartApiFailureMessage, } from "./multipartStageFailures.js";
|
|
3
|
+
function isMultipartStageStatus(stage) {
|
|
4
|
+
return "completed" in stage && typeof stage.completed === "boolean";
|
|
5
|
+
}
|
|
6
|
+
async function resumeMultipartStage(input, resumeStageId) {
|
|
7
|
+
const resumed = await input.apiClient.getMultipartBlobStage(resumeStageId);
|
|
8
|
+
if (!resumed) {
|
|
9
|
+
await assertMultipartStageReplaceable({
|
|
10
|
+
apiClient: input.apiClient,
|
|
11
|
+
onStageUnavailable: input.onStageUnavailable,
|
|
12
|
+
stageId: resumeStageId,
|
|
13
|
+
});
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (resumed.stageId !== resumeStageId) {
|
|
17
|
+
throw new Error(`Multipart blob resume response does not match stage ${resumeStageId}.`);
|
|
18
|
+
}
|
|
19
|
+
// A successful lookup is authoritative: a metadata mismatch indicates
|
|
20
|
+
// inconsistent persisted state or a bad server response, not proof that
|
|
21
|
+
// the existing stage is safe to abandon.
|
|
22
|
+
if (resumed.organizationId !== input.organizationId ||
|
|
23
|
+
resumed.byteLength !== input.encryption.byteLength ||
|
|
24
|
+
resumed.sha256 !== input.encryption.sha256) {
|
|
25
|
+
throw new Error(`Multipart blob resume stage ${resumeStageId} does not match the requested organization or bytes.`);
|
|
26
|
+
}
|
|
27
|
+
return resumed;
|
|
28
|
+
}
|
|
29
|
+
async function initiateWithFirstParts(input, firstParts) {
|
|
30
|
+
const { byteLength, sha256 } = input.encryption;
|
|
31
|
+
const stage = await input.apiClient.initiateMultipartBlobStage({
|
|
32
|
+
organizationId: input.organizationId,
|
|
33
|
+
byteLength,
|
|
34
|
+
firstParts: [...firstParts.descriptors],
|
|
35
|
+
sha256,
|
|
36
|
+
});
|
|
37
|
+
if (!stage) {
|
|
38
|
+
throw new Error(multipartApiFailureMessage({
|
|
39
|
+
apiClient: input.apiClient,
|
|
40
|
+
fallback: `Multipart blob stage initiation failed for ${byteLength.toLocaleString()} bytes.`,
|
|
41
|
+
request: { method: "POST", path: "/blobs/stages/multipart" },
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
if (stage.organizationId !== input.organizationId ||
|
|
45
|
+
stage.byteLength !== byteLength ||
|
|
46
|
+
stage.sha256 !== sha256) {
|
|
47
|
+
throw new Error("Multipart blob stage initiation response does not match its request.");
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
prepared: firstParts.prepared,
|
|
51
|
+
sources: readIssuedPartSources(firstParts.descriptors, stage),
|
|
52
|
+
status: isMultipartStageStatus(stage)
|
|
53
|
+
? stage
|
|
54
|
+
: { ...stage, completed: false },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// A new stage carries the parts the upload starts with, so their upload
|
|
58
|
+
// sources come back with the stage instead of one authorization each. Their
|
|
59
|
+
// bytes hold transfer-pool slots until uploaded, or until initiation fails.
|
|
60
|
+
async function initiateMultipartStage(input) {
|
|
61
|
+
const firstParts = await prepareFirstMultipartParts(input.apiClient, input.encryption, input.concurrency);
|
|
62
|
+
try {
|
|
63
|
+
return await initiateWithFirstParts(input, firstParts);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
releasePreparedParts(firstParts.prepared);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export async function resolveMultipartStage(input) {
|
|
71
|
+
const resumeStageId = input.multipart.resumeStageId;
|
|
72
|
+
const resumed = resumeStageId
|
|
73
|
+
? await resumeMultipartStage(input, resumeStageId)
|
|
74
|
+
: null;
|
|
75
|
+
return resumed
|
|
76
|
+
? {
|
|
77
|
+
prepared: new Map(),
|
|
78
|
+
sources: { apiTransport: false, issued: new Map() },
|
|
79
|
+
status: resumed,
|
|
80
|
+
}
|
|
81
|
+
: initiateMultipartStage(input);
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=multipartStageResolution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipartStageResolution.js","sourceRoot":"","sources":["multipartStageResolution.ts"],"names":[],"mappings":"AASA,OAAO,EAIL,0BAA0B,EAC1B,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,+BAA+B,EAC/B,0BAA0B,GAC3B,MAAM,0BAA0B,CAAC;AAElC,SAAS,sBAAsB,CAC7B,KAA4E;IAE5E,OAAO,WAAW,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,SAAS,KAAK,SAAS,CAAC;AACtE,CAAC;AAmBD,KAAK,UAAU,oBAAoB,CACjC,KAAiC,EACjC,aAAqB;IAErB,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,qBAAqB,CAAC,aAAa,CAAC,CAAC;IAC3E,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,+BAA+B,CAAC;YACpC,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;YAC5C,OAAO,EAAE,aAAa;SACvB,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,OAAO,CAAC,OAAO,KAAK,aAAa,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,uDAAuD,aAAa,GAAG,CACxE,CAAC;IACJ,CAAC;IACD,sEAAsE;IACtE,wEAAwE;IACxE,yCAAyC;IACzC,IACE,OAAO,CAAC,cAAc,KAAK,KAAK,CAAC,cAAc;QAC/C,OAAO,CAAC,UAAU,KAAK,KAAK,CAAC,U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AE,SAAS,EAAE,KAAK,EAAE;KACnC,CAAC;AACJ,CAAC;AAED,wEAAwE;AACxE,4EAA4E;AAC5E,4EAA4E;AAC5E,KAAK,UAAU,sBAAsB,CACnC,KAAiC;IAEjC,MAAM,UAAU,GAAG,MAAM,0BAA0B,CACjD,KAAK,CAAC,SAAS,EACf,KAAK,CAAC,UAAU,EAChB,KAAK,CAAC,WAAW,CAClB,CAAC;IACF,IAAI,CAAC;QACH,OAAO,MAAM,sBAAsB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IACzD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,oBAAoB,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QAC1C,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,KAAiC;IAEjC,MAAM,aAAa,GAAG,KAAK,CAAC,SAAS,CAAC,aAAa,CAAC;IACpD,MAAM,OAAO,GAAG,aAAa;QAC3B,CAAC,CAAC,MAAM,oBAAoB,CAAC,KAAK,EAAE,aAAa,CAAC;QAClD,CAAC,CAAC,IAAI,CAAC;IACT,OAAO,OAAO;QACZ,CAAC,CAAC;YACE,QAAQ,EAAE,IAAI,GAAG,EAAE;YACnB,OAAO,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,GAAG,EAAE,EAAE;YACnD,MAAM,EAAE,OAAO;SAChB;QACH,CAAC,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;AACpC,CAAC","sourcesContent":["import type {\n InitiateMultipartBlobStageResponse,\n MultipartBlobStageStatusResponse,\n} from \"@tearleads/validators/response\";\nimport type { BlobEncryptionPlan } from \"../../data/documents/blob/shared/crypto\";\nimport type {\n BlobAttachmentApi,\n UploadDocumentAttachmentInput,\n} from \"../../data/documents/blob/shared/types\";\nimport {\n type MultipartFirstParts,\n type MultipartPartSources,\n type PreparedFirstPart,\n prepareFirstMultipartParts,\n readIssuedPartSources,\n releasePreparedParts,\n} from \"./multipartFirstParts\";\nimport {\n assertMultipartStageReplaceable,\n multipartApiFailureMessage,\n} from \"./multipartStageFailures\";\n\nfunction isMultipartStageStatus(\n stage: InitiateMultipartBlobStageResponse | MultipartBlobStageStatusResponse,\n): stage is MultipartBlobStageStatusResponse {\n return \"completed\" in stage && typeof stage.completed === \"boolean\";\n}\n\ninterface ResolveMultipartStageInput {\n readonly apiClient: BlobAttachmentApi;\n readonly concurrency: number;\n readonly encryption: BlobEncryptionPlan;\n readonly multipart: NonNullable<UploadDocumentAttachmentInput[\"multipart\"]>;\n readonly organizationId: string;\n readonly onStageUnavailable?:\n | ((stageId: string) => Promise<void>)\n | undefined;\n}\n\ninterface ResolvedMultipartStage {\n readonly prepared: Map<number, PreparedFirstPart>;\n readonly sources: MultipartPartSources;\n readonly status: MultipartBlobStageStatusResponse;\n}\n\nasync function resumeMultipartStage(\n input: ResolveMultipartStageInput,\n resumeStageId: string,\n): Promise<MultipartBlobStageStatusResponse | null> {\n const resumed = await input.apiClient.getMultipartBlobStage(resumeStageId);\n if (!resumed) {\n await assertMultipartStageReplaceable({\n apiClient: input.apiClient,\n onStageUnavailable: input.onStageUnavailable,\n stageId: resumeStageId,\n });\n return null;\n }\n if (resumed.stageId !== resumeStageId) {\n throw new Error(\n `Multipart blob resume response does not match stage ${resumeStageId}.`,\n );\n }\n // A successful lookup is authoritative: a metadata mismatch indicates\n // inconsistent persisted state or a bad server response, not proof that\n // the existing stage is safe to abandon.\n if (\n resumed.organizationId !== input.organizationId ||\n resumed.byteLength !== input.encryption.byteLength ||\n resumed.sha256 !== input.encryption.sha256\n ) {\n throw new Error(\n `Multipart blob resume stage ${resumeStageId} does not match the requested organization or bytes.`,\n );\n }\n return resumed;\n}\n\nasync function initiateWithFirstParts(\n input: ResolveMultipartStageInput,\n firstParts: MultipartFirstParts,\n): Promise<ResolvedMultipartStage> {\n const { byteLength, sha256 } = input.encryption;\n const stage = await input.apiClient.initiateMultipartBlobStage({\n organizationId: input.organizationId,\n byteLength,\n firstParts: [...firstParts.descriptors],\n sha256,\n });\n if (!stage) {\n throw new Error(\n multipartApiFailureMessage({\n apiClient: input.apiClient,\n fallback: `Multipart blob stage initiation failed for ${byteLength.toLocaleString()} bytes.`,\n request: { method: \"POST\", path: \"/blobs/stages/multipart\" },\n }),\n );\n }\n if (\n stage.organizationId !== input.organizationId ||\n stage.byteLength !== byteLength ||\n stage.sha256 !== sha256\n ) {\n throw new Error(\n \"Multipart blob stage initiation response does not match its request.\",\n );\n }\n return {\n prepared: firstParts.prepared,\n sources: readIssuedPartSources(firstParts.descriptors, stage),\n status: isMultipartStageStatus(stage)\n ? stage\n : { ...stage, completed: false },\n };\n}\n\n// A new stage carries the parts the upload starts with, so their upload\n// sources come back with the stage instead of one authorization each. Their\n// bytes hold transfer-pool slots until uploaded, or until initiation fails.\nasync function initiateMultipartStage(\n input: ResolveMultipartStageInput,\n): Promise<ResolvedMultipartStage> {\n const firstParts = await prepareFirstMultipartParts(\n input.apiClient,\n input.encryption,\n input.concurrency,\n );\n try {\n return await initiateWithFirstParts(input, firstParts);\n } catch (error) {\n releasePreparedParts(firstParts.prepared);\n throw error;\n }\n}\n\nexport async function resolveMultipartStage(\n input: ResolveMultipartStageInput,\n): Promise<ResolvedMultipartStage> {\n const resumeStageId = input.multipart.resumeStageId;\n const resumed = resumeStageId\n ? await resumeMultipartStage(input, resumeStageId)\n : null;\n return resumed\n ? {\n prepared: new Map(),\n sources: { apiTransport: false, issued: new Map() },\n status: resumed,\n }\n : initiateMultipartStage(input);\n}\n"]}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MAX_BLOB_CHUNK_COUNT } from "../../internal/crypto/index.js";
|
|
2
2
|
import { MAX_MULTIPART_BLOB_PART_BYTES } from "../../internal/validators/util/index.js";
|
|
3
3
|
import { BLOB_TRANSFER_CONCURRENCY, runBlobTransfer, } from "../../data/blobs/transferPool.js";
|
|
4
|
-
import {
|
|
4
|
+
import { releasePreparedParts, sha256BytesHex, takePartSource, } from "./multipartFirstParts.js";
|
|
5
|
+
import { multipartApiFailureMessage, multipartStagePath, } from "./multipartStageFailures.js";
|
|
6
|
+
import { resolveMultipartStage } from "./multipartStageResolution.js";
|
|
5
7
|
const DEFAULT_MULTIPART_UPLOAD_CONCURRENCY = 4;
|
|
6
8
|
const MAX_MULTIPART_UPLOAD_CONCURRENCY = 4;
|
|
7
9
|
// Fetch exposes no upload progress on every host. Allow a slow shared uplink
|
|
@@ -19,9 +21,6 @@ function multipartPartRequest(input) {
|
|
|
19
21
|
? authorization
|
|
20
22
|
: { method: "PUT", path: `${basePath}/bytes` };
|
|
21
23
|
}
|
|
22
|
-
async function sha256BytesHex(bytes) {
|
|
23
|
-
return bytesToHex(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)));
|
|
24
|
-
}
|
|
25
24
|
function normalizeMultipartUploadConcurrency(value) {
|
|
26
25
|
if (value === undefined) {
|
|
27
26
|
return DEFAULT_MULTIPART_UPLOAD_CONCURRENCY;
|
|
@@ -52,6 +51,8 @@ async function uploadMultipartPartTasks(input) {
|
|
|
52
51
|
return;
|
|
53
52
|
}
|
|
54
53
|
try {
|
|
54
|
+
// A first part already holds the pool slot its prepared bytes use.
|
|
55
|
+
const heldPermit = input.prepared.get(task.partIndex)?.permit;
|
|
55
56
|
await runBlobTransfer(input.apiClient, async (signal) => {
|
|
56
57
|
if (failed)
|
|
57
58
|
return;
|
|
@@ -61,6 +62,8 @@ async function uploadMultipartPartTasks(input) {
|
|
|
61
62
|
const uploaded = await uploadMultipartPartBytes({
|
|
62
63
|
apiClient: input.apiClient,
|
|
63
64
|
encryption: input.encryption,
|
|
65
|
+
prepared: input.prepared,
|
|
66
|
+
sources: input.sources,
|
|
64
67
|
stageId: input.stageId,
|
|
65
68
|
task,
|
|
66
69
|
uploadId: input.uploadId,
|
|
@@ -95,7 +98,7 @@ async function uploadMultipartPartTasks(input) {
|
|
|
95
98
|
}
|
|
96
99
|
}, 120_000 +
|
|
97
100
|
Math.ceil((task.byteLength * BLOB_TRANSFER_CONCURRENCY * 1000) /
|
|
98
|
-
MULTIPART_TRANSFER_BYTES_PER_SECOND));
|
|
101
|
+
MULTIPART_TRANSFER_BYTES_PER_SECOND), heldPermit);
|
|
99
102
|
}
|
|
100
103
|
catch (error) {
|
|
101
104
|
recordFailure(error);
|
|
@@ -108,18 +111,30 @@ async function uploadMultipartPartTasks(input) {
|
|
|
108
111
|
throw firstFailure.error;
|
|
109
112
|
}
|
|
110
113
|
}
|
|
114
|
+
// A first part was encrypted once before the stage existed; hand its bytes
|
|
115
|
+
// over (and release them) instead of encrypting it again.
|
|
116
|
+
async function encryptedPart(input) {
|
|
117
|
+
const prepared = input.prepared.get(input.task.partIndex);
|
|
118
|
+
if (prepared) {
|
|
119
|
+
input.prepared.delete(input.task.partIndex);
|
|
120
|
+
return prepared;
|
|
121
|
+
}
|
|
122
|
+
const bytes = await input.encryption.encryptPart(input.task.partIndex);
|
|
123
|
+
if (bytes.byteLength !== input.task.byteLength) {
|
|
124
|
+
throw new Error(`Encrypted blob part ${input.task.partNumber} byte length mismatch: expected ${input.task.byteLength.toLocaleString()}, received ${bytes.byteLength.toLocaleString()}.`);
|
|
125
|
+
}
|
|
126
|
+
return { bytes, sha256: await sha256BytesHex(bytes) };
|
|
127
|
+
}
|
|
111
128
|
async function uploadMultipartPartBytes(input) {
|
|
112
129
|
input.signal.throwIfAborted();
|
|
113
|
-
const encryptedPartBytes = await
|
|
114
|
-
if (encryptedPartBytes.byteLength !== input.task.byteLength) {
|
|
115
|
-
throw new Error(`Encrypted blob part ${input.task.partNumber} byte length mismatch: expected ${input.task.byteLength.toLocaleString()}, received ${encryptedPartBytes.byteLength.toLocaleString()}.`);
|
|
116
|
-
}
|
|
117
|
-
const sha256 = await sha256BytesHex(encryptedPartBytes);
|
|
130
|
+
const { bytes: encryptedPartBytes, sha256 } = await encryptedPart(input);
|
|
118
131
|
input.signal.throwIfAborted();
|
|
132
|
+
const source = takePartSource(input.sources, input.task.partNumber);
|
|
119
133
|
const uploaded = await input.apiClient.uploadMultipartBlobPartBytes(input.stageId, input.task.partNumber, {
|
|
120
134
|
byteLength: encryptedPartBytes.byteLength,
|
|
121
135
|
encryptedBytes: encryptedPartBytes,
|
|
122
136
|
sha256,
|
|
137
|
+
...(source === undefined ? {} : { source }),
|
|
123
138
|
uploadId: input.uploadId,
|
|
124
139
|
}, { signal: input.signal });
|
|
125
140
|
input.signal.throwIfAborted();
|
|
@@ -135,9 +150,6 @@ async function uploadMultipartPartBytes(input) {
|
|
|
135
150
|
function uploadedPartsByPartNumber(parts) {
|
|
136
151
|
return new Map(parts.map((part) => [part.partNumber, part]));
|
|
137
152
|
}
|
|
138
|
-
function isMultipartStageStatus(stage) {
|
|
139
|
-
return "completed" in stage && typeof stage.completed === "boolean";
|
|
140
|
-
}
|
|
141
153
|
/**
|
|
142
154
|
* Plans encrypted part indices without materializing their bytes, reusing any
|
|
143
155
|
* already-staged parts and seeding cumulative progress for resumed uploads.
|
|
@@ -174,56 +186,6 @@ function planMultipartParts(input) {
|
|
|
174
186
|
}
|
|
175
187
|
return { completeParts, progress, uploadTasks };
|
|
176
188
|
}
|
|
177
|
-
async function resolveMultipartStageStatus(input) {
|
|
178
|
-
const resumeStageId = input.multipart.resumeStageId;
|
|
179
|
-
if (resumeStageId) {
|
|
180
|
-
const resumed = await input.apiClient.getMultipartBlobStage(resumeStageId);
|
|
181
|
-
if (!resumed) {
|
|
182
|
-
await assertMultipartStageReplaceable({
|
|
183
|
-
apiClient: input.apiClient,
|
|
184
|
-
onStageUnavailable: input.onStageUnavailable,
|
|
185
|
-
stageId: resumeStageId,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
if (resumed && resumed.stageId !== resumeStageId) {
|
|
189
|
-
throw new Error(`Multipart blob resume response does not match stage ${resumeStageId}.`);
|
|
190
|
-
}
|
|
191
|
-
if (resumed) {
|
|
192
|
-
// A successful lookup is authoritative: a metadata mismatch indicates
|
|
193
|
-
// inconsistent persisted state or a bad server response, not proof that
|
|
194
|
-
// the existing stage is safe to abandon.
|
|
195
|
-
if (resumed.organizationId !== input.organizationId ||
|
|
196
|
-
resumed.byteLength !== input.byteLength ||
|
|
197
|
-
resumed.sha256 !== input.sha256) {
|
|
198
|
-
throw new Error(`Multipart blob resume stage ${resumeStageId} does not match the requested organization or bytes.`);
|
|
199
|
-
}
|
|
200
|
-
return resumed;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
const stage = await input.apiClient.initiateMultipartBlobStage({
|
|
204
|
-
organizationId: input.organizationId,
|
|
205
|
-
byteLength: input.byteLength,
|
|
206
|
-
sha256: input.sha256,
|
|
207
|
-
});
|
|
208
|
-
if (!stage) {
|
|
209
|
-
throw new Error(multipartApiFailureMessage({
|
|
210
|
-
apiClient: input.apiClient,
|
|
211
|
-
fallback: `Multipart blob stage initiation failed for ${input.byteLength.toLocaleString()} bytes.`,
|
|
212
|
-
request: { method: "POST", path: "/blobs/stages/multipart" },
|
|
213
|
-
}));
|
|
214
|
-
}
|
|
215
|
-
if (stage.organizationId !== input.organizationId ||
|
|
216
|
-
stage.byteLength !== input.byteLength ||
|
|
217
|
-
stage.sha256 !== input.sha256) {
|
|
218
|
-
throw new Error("Multipart blob stage initiation response does not match its request.");
|
|
219
|
-
}
|
|
220
|
-
return isMultipartStageStatus(stage)
|
|
221
|
-
? stage
|
|
222
|
-
: {
|
|
223
|
-
...stage,
|
|
224
|
-
completed: false,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
189
|
function assertMultipartPlanLimits(encryption) {
|
|
228
190
|
if (encryption.partCount > MAX_BLOB_CHUNK_COUNT) {
|
|
229
191
|
throw new Error(`Multipart blob upload would require ${encryption.partCount.toLocaleString()} parts; the maximum is ${MAX_BLOB_CHUNK_COUNT.toLocaleString()} parts.`);
|
|
@@ -239,14 +201,24 @@ export async function stageMultipartBlobAttachment(input) {
|
|
|
239
201
|
throw new Error("Multipart part size must match the encryption chunk size exactly.");
|
|
240
202
|
}
|
|
241
203
|
assertMultipartPlanLimits(input.encryption);
|
|
242
|
-
const
|
|
204
|
+
const concurrency = normalizeMultipartUploadConcurrency(input.multipart.uploadConcurrency);
|
|
205
|
+
const resolved = await resolveMultipartStage({
|
|
243
206
|
apiClient: input.apiClient,
|
|
244
|
-
|
|
207
|
+
concurrency,
|
|
208
|
+
encryption: input.encryption,
|
|
245
209
|
multipart: input.multipart,
|
|
246
210
|
organizationId: input.organizationId,
|
|
247
211
|
onStageUnavailable: input.onStageUnavailable,
|
|
248
|
-
sha256: input.encryption.sha256,
|
|
249
212
|
});
|
|
213
|
+
try {
|
|
214
|
+
return await uploadResolvedMultipartStage(input, concurrency, resolved);
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
// First parts no worker consumed (an early failure) return their slots.
|
|
218
|
+
releasePreparedParts(resolved.prepared);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async function uploadResolvedMultipartStage(input, concurrency, { prepared, sources, status, }) {
|
|
250
222
|
// Surface the stage id before any parts upload so the caller can persist it;
|
|
251
223
|
// an upload interrupted mid-flight can then resume this same stage.
|
|
252
224
|
await input.onStageResolved?.({
|
|
@@ -278,13 +250,15 @@ export async function stageMultipartBlobAttachment(input) {
|
|
|
278
250
|
await uploadMultipartPartTasks({
|
|
279
251
|
apiClient: input.apiClient,
|
|
280
252
|
completeParts,
|
|
281
|
-
concurrency
|
|
253
|
+
concurrency,
|
|
282
254
|
encryption: input.encryption,
|
|
283
255
|
onPartUploaded: (task) => {
|
|
284
256
|
progress.partsCompleted += 1;
|
|
285
257
|
progress.bytesUploaded += task.byteLength;
|
|
286
258
|
reportMultipartProgress();
|
|
287
259
|
},
|
|
260
|
+
prepared,
|
|
261
|
+
sources,
|
|
288
262
|
stageId: status.stageId,
|
|
289
263
|
tasks: uploadTasks,
|
|
290
264
|
uploadId: status.uploadId,
|
|
@@ -293,26 +267,8 @@ export async function stageMultipartBlobAttachment(input) {
|
|
|
293
267
|
if (committedParts.length !== input.encryption.partCount) {
|
|
294
268
|
throw new Error(`Multipart blob upload incomplete for stage ${status.stageId}: committed ${committedParts.length.toLocaleString()} of ${input.encryption.partCount.toLocaleString()} parts.`);
|
|
295
269
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
});
|
|
300
|
-
if (!completed) {
|
|
301
|
-
throw new Error(multipartApiFailureMessage({
|
|
302
|
-
apiClient: input.apiClient,
|
|
303
|
-
fallback: `Multipart blob stage completion failed for stage ${status.stageId} with ${committedParts.length.toLocaleString()} committed parts.`,
|
|
304
|
-
request: {
|
|
305
|
-
method: "POST",
|
|
306
|
-
path: `${multipartStagePath(status.stageId)}/complete`,
|
|
307
|
-
},
|
|
308
|
-
}));
|
|
309
|
-
}
|
|
310
|
-
if (completed.organizationId !== input.organizationId ||
|
|
311
|
-
completed.stageId !== status.stageId ||
|
|
312
|
-
completed.byteLength !== input.encryption.byteLength ||
|
|
313
|
-
completed.sha256 !== input.encryption.sha256) {
|
|
314
|
-
throw new Error(`Multipart blob stage ${status.stageId} completion response does not match its request.`);
|
|
315
|
-
}
|
|
316
|
-
return completed.stageId;
|
|
270
|
+
// Binding the attachment completes the stage: the API assembles these parts
|
|
271
|
+
// and verifies the object's length and SHA-256 before promoting it.
|
|
272
|
+
return status.stageId;
|
|
317
273
|
}
|
|
318
274
|
//# sourceMappingURL=multipartUpload.js.map
|