@@ -9,6 +9,7 @@ import { openWorkspaceDatabase } from "./db/initialize.js";
99import {
1010 createCollaborationRuntime ,
1111 type CollaborationRuntime ,
12+ type UnitSnapshotStore ,
1213 type UnitStore ,
1314} from "./integrations/univer/unit-store.js" ;
1415import { createWorktreeBackend } from "./integrations/univer/worktree-store.js" ;
@@ -97,6 +98,11 @@ import {
9798 UniverAssetsRepository ,
9899 type UniverAssetsModule ,
99100} from "./modules/univer-assets/index.js" ;
101+ import {
102+ createExchangeModule ,
103+ createExchangeRouter ,
104+ type ExchangeModule ,
105+ } from "./modules/exchange/index.js" ;
100106
101107export interface WorkspaceApplication {
102108 readonly app : express . Express ;
@@ -113,6 +119,7 @@ export interface WorkspaceApplication {
113119 readonly operations : OperationsModule ;
114120 readonly blobs : BlobsModule ;
115121 readonly univerAssets : UniverAssetsModule ;
122+ readonly exchange : ExchangeModule ;
116123 attachWebSocket ( server : Server ) : void ;
117124 closeRealtime ( ) : Promise < void > ;
118125 close ( ) : Promise < void > ;
@@ -122,6 +129,7 @@ export function createWorkspaceApplication(
122129 config : WorkspaceConfig ,
123130 dependencies : {
124131 readonly unitStore ?: UnitStore ;
132+ readonly unitSnapshotStore ?: UnitSnapshotStore ;
125133 readonly worktreeBackend ?: WorktreeBackend ;
126134 readonly blobStore ?: BlobStore ;
127135 readonly githubOAuthProvider ?: GitHubOAuthProvider ;
@@ -136,6 +144,10 @@ export function createWorkspaceApplication(
136144 ( collaboration = createCollaborationRuntime (
137145 config . collaborationDatabaseFilename
138146 ) ) . unitStore ;
147+ const unitSnapshotStore =
148+ dependencies . unitSnapshotStore ??
149+ collaboration ?. unitSnapshotStore ??
150+ unavailableUnitSnapshotStore ( ) ;
139151 const oauthStateSecret =
140152 dependencies . oauthStateSecret ??
141153 config . githubOAuth ?. clientSecret ??
@@ -219,6 +231,13 @@ export function createWorkspaceApplication(
219231 resources,
220232 worktrees,
221233 } ) ;
234+ const exchange = createExchangeModule ( {
235+ access,
236+ resources,
237+ spaces,
238+ snapshots : unitSnapshotStore ,
239+ store : blobStore ,
240+ } ) ;
222241 const collaborationGateway : CollaborationGateway | null = collaboration
223242 ? createCollaborationGateway ( {
224243 service : collaboration . service ,
@@ -283,6 +302,10 @@ export function createWorkspaceApplication(
283302 "/api" ,
284303 createWorktreesRouter ( { identity, worktrees } )
285304 ) ;
305+ app . use (
306+ "/universer-api" ,
307+ createExchangeRouter ( { identity, exchange } )
308+ ) ;
286309 app . use (
287310 "/universer-api" ,
288311 createUniverAssetsRouter ( { identity, assets : univerAssets } )
@@ -318,6 +341,7 @@ export function createWorkspaceApplication(
318341 operations,
319342 blobs,
320343 univerAssets,
344+ exchange,
321345 attachWebSocket ( server ) {
322346 collaborationGateway ?. attachWebSocket ( server ) ;
323347 } ,
@@ -329,15 +353,29 @@ export function createWorkspaceApplication(
329353 await collaborationGateway ?. dispose ( ) ;
330354 } finally {
331355 try {
332- await collaboration ? .dispose ( ) ;
356+ await exchange . dispose ( ) ;
333357 } finally {
334- database . close ( ) ;
358+ try {
359+ await collaboration ?. dispose ( ) ;
360+ } finally {
361+ database . close ( ) ;
362+ }
335363 }
336364 }
337365 } ,
338366 } ;
339367}
340368
369+ function unavailableUnitSnapshotStore ( ) : UnitSnapshotStore {
370+ return {
371+ async materialize ( ) {
372+ throw new Error (
373+ "A Collaboration snapshot store is required for Unit export."
374+ ) ;
375+ } ,
376+ } ;
377+ }
378+
341379function unavailableWorktreeBackend ( ) : WorktreeBackend {
342380 const unavailable = async ( ) : Promise < never > => {
343381 throw new Error (
0 commit comments