@fluidframework/local-driver 3.1.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/api-report/local-driver.alpha.api.md +21 -7
- package/dist/alpha.d.ts +4 -1
- package/dist/ephemeralService.d.ts +126 -74
- package/dist/ephemeralService.d.ts.map +1 -1
- package/dist/ephemeralService.js +125 -19
- package/dist/ephemeralService.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/localSessionStorageDb.d.ts.map +1 -1
- package/dist/localSessionStorageDb.js +19 -8
- package/dist/localSessionStorageDb.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/lib/alpha.d.ts +4 -1
- package/lib/ephemeralService.d.ts +126 -74
- package/lib/ephemeralService.d.ts.map +1 -1
- package/lib/ephemeralService.js +123 -19
- package/lib/ephemeralService.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/localSessionStorageDb.d.ts.map +1 -1
- package/lib/localSessionStorageDb.js +19 -8
- package/lib/localSessionStorageDb.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/package.json +16 -17
- package/src/ephemeralService.ts +278 -109
- package/src/index.ts +5 -1
- package/src/localSessionStorageDb.ts +23 -7
- package/src/packageVersion.ts +1 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { IRequest } from "@fluidframework/core-interfaces";
|
|
6
6
|
import { type ErasedBaseType } from "@fluidframework/core-interfaces/internal";
|
|
7
|
-
import { type DataStoreKind, type DataStoreRegistry, type FluidContainerAttached, type FluidContainerWithService, type
|
|
7
|
+
import { type DataStoreKind, type DataStoreRegistry, type FluidContainerAttached, type FluidContainerWithService, type ServiceClient, type ServiceOptions } from "@fluidframework/driver-definitions/internal";
|
|
8
8
|
import { ServiceContainerBase } from "@fluidframework/runtime-utils/internal";
|
|
9
9
|
/**
|
|
10
10
|
* Starts and returns a new {@link EphemeralService}.
|
|
@@ -18,6 +18,20 @@ import { ServiceContainerBase } from "@fluidframework/runtime-utils/internal";
|
|
|
18
18
|
* @alpha
|
|
19
19
|
*/
|
|
20
20
|
export declare function startEphemeralService(isDefault?: boolean): EphemeralService;
|
|
21
|
+
/**
|
|
22
|
+
* Gets the session-storage-backed local Fluid service for the current JavaScript realm, creating it on first use.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Attached documents remain available after a page reload within the same browser tab. Repeated calls return
|
|
26
|
+
* the same service instance within the current JavaScript realm.
|
|
27
|
+
*
|
|
28
|
+
* This service is only available in browser environments that provide `sessionStorage`.
|
|
29
|
+
* @returns A local service backed by browser session storage.
|
|
30
|
+
* @alpha
|
|
31
|
+
*/
|
|
32
|
+
export declare function getSessionService(): SessionService;
|
|
33
|
+
/** Closes and clears the current session service for test isolation. */
|
|
34
|
+
export declare function resetSessionServiceForTesting(): Promise<void>;
|
|
21
35
|
/**
|
|
22
36
|
* Cleans up the service passed in {@link startEphemeralService}, or the {@link getDefaultEphemeralService|default} if none is passed.
|
|
23
37
|
* @remarks
|
|
@@ -34,23 +48,91 @@ export declare function cleanupEphemeralService(service?: EphemeralService): Pro
|
|
|
34
48
|
*/
|
|
35
49
|
export declare function getDefaultEphemeralService(): EphemeralService;
|
|
36
50
|
/**
|
|
37
|
-
* Internal
|
|
38
|
-
*
|
|
51
|
+
* Internal options for creating a local service client, extending
|
|
52
|
+
* {@link @fluidframework/driver-definitions#ServiceOptions} with the service the client should connect to.
|
|
39
53
|
* @input
|
|
40
54
|
* @internal
|
|
41
55
|
*/
|
|
42
|
-
export interface
|
|
56
|
+
export interface LocalServiceOptions<TService extends LocalService = LocalService> extends ServiceOptions {
|
|
43
57
|
/**
|
|
44
|
-
*
|
|
58
|
+
* The service instance to connect to.
|
|
45
59
|
*/
|
|
46
|
-
readonly
|
|
60
|
+
readonly service: TService;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Internal options for creating a {@link LocalServiceClient} connected to an {@link EphemeralService}.
|
|
64
|
+
* @input
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
export interface EphemeralServiceOptions extends LocalServiceOptions<EphemeralService> {
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A local Fluid service with an explicitly managed lifecycle.
|
|
71
|
+
* @remarks
|
|
72
|
+
* There are two implementations of this interface with different document lifetimes:
|
|
73
|
+
* {@link EphemeralService} and {@link SessionService}.
|
|
74
|
+
*
|
|
75
|
+
* @typeParam TClient - The type of client this service creates.
|
|
76
|
+
* @alpha @sealed
|
|
77
|
+
*/
|
|
78
|
+
export interface LocalService<out TClient extends ServiceClient = LocalServiceClient> extends ErasedBaseType<readonly ["LocalService", TClient]> {
|
|
47
79
|
/**
|
|
48
|
-
*
|
|
80
|
+
* Lists the IDs of documents currently stored by this service.
|
|
81
|
+
*/
|
|
82
|
+
listDocumentIds(): Promise<readonly string[]>;
|
|
83
|
+
/**
|
|
84
|
+
* Deletes a stored document.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* Deletion is only allowed when this service has no open containers because resetting the local
|
|
88
|
+
* server invalidates all of its active connections. Content-addressed summary data shared with other
|
|
89
|
+
* documents may be retained until {@link LocalService.deleteAllDocuments} is called.
|
|
90
|
+
* Only one call to {@link LocalService.deleteDocument} or {@link LocalService.deleteAllDocuments}
|
|
91
|
+
* may be in progress at a time.
|
|
92
|
+
*
|
|
93
|
+
* @param id - The ID of the document to delete.
|
|
94
|
+
* @throws A `UsageError` if the service is closed, another document deletion is in progress, or the
|
|
95
|
+
* service has open containers.
|
|
96
|
+
*/
|
|
97
|
+
deleteDocument(id: string): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Deletes all documents stored by this service.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* Deletion is only allowed when this service has no open containers because resetting the local
|
|
103
|
+
* server invalidates all of its active connections.
|
|
104
|
+
* Only one call to {@link LocalService.deleteDocument} or {@link LocalService.deleteAllDocuments}
|
|
105
|
+
* may be in progress at a time.
|
|
106
|
+
* @throws A `UsageError` if the service is closed, another document deletion is in progress, or the
|
|
107
|
+
* service has open containers.
|
|
108
|
+
*/
|
|
109
|
+
deleteAllDocuments(): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Drives all containers connected to this service toward convergence, processing pending operations and
|
|
112
|
+
* waiting for all dirty containers to save.
|
|
113
|
+
*
|
|
114
|
+
* @param timeoutMilliseconds - The maximum time to wait for containers to quiesce, in milliseconds. Defaults to 30_000.
|
|
115
|
+
* @throws A `UsageError` if the containers do not quiesce before the timeout expires.
|
|
116
|
+
*
|
|
117
|
+
* @privateRemarks
|
|
118
|
+
* This is a best-effort implementation simplified from `LoaderContainerTracker.ensureSynchronized`.
|
|
119
|
+
* Currently it does not perform receiver-side sequence-number quiescence or wait for join/leave (audience) ops.
|
|
120
|
+
* See `LoaderContainerTracker.ensureSynchronized` for the fuller version this is based on.
|
|
121
|
+
*/
|
|
122
|
+
synchronize(timeoutMilliseconds?: number): Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a client connected to this service.
|
|
125
|
+
*
|
|
126
|
+
* @param options - Collaboration options for the client.
|
|
127
|
+
*/
|
|
128
|
+
newClient(options: ServiceOptions): TClient;
|
|
129
|
+
/**
|
|
130
|
+
* A client connected to this service using the default options.
|
|
49
131
|
*/
|
|
50
|
-
readonly
|
|
132
|
+
readonly defaultClient: TClient;
|
|
51
133
|
}
|
|
52
134
|
/**
|
|
53
|
-
* An in-memory Fluid service that can produce connected {@link
|
|
135
|
+
* An in-memory Fluid service that can produce connected {@link LocalServiceClient}s.
|
|
54
136
|
* @remarks
|
|
55
137
|
* All documents created through clients connected to a given `EphemeralService` are held in-memory by that service.
|
|
56
138
|
* Closing the service (via {@link EphemeralService.close} or {@link cleanupEphemeralService}) closes the connections
|
|
@@ -68,20 +150,20 @@ export interface EphemeralServiceOptions extends ServiceOptions {
|
|
|
68
150
|
* document with different `oldestSupportedClient` values.
|
|
69
151
|
* This also exposes a place to put APIs for preloading and exporting document contents in the future.
|
|
70
152
|
*
|
|
71
|
-
* This is an erased type: its only implementation is the module-private
|
|
153
|
+
* This is an erased type: its only implementation is the module-private `LocalServiceImplementation`, which holds
|
|
72
154
|
* the mutable server and container state so it does not appear on this public type.
|
|
73
155
|
*
|
|
74
156
|
* TODO: formalize this lifecycle with an interface which documents these stages.
|
|
75
157
|
* Lifecycle:
|
|
76
158
|
* The intended lifecycle of an {@link EphemeralService} follows roughly the same pattern as containers:
|
|
77
159
|
*
|
|
78
|
-
* 1. Open: accepts connections from {@link
|
|
160
|
+
* 1. Open: accepts connections from {@link LocalServiceClient}s, which can create and load containers.
|
|
79
161
|
* Might have timers and event registrations which can trigger asynchronous work, and retain the object in memory.
|
|
80
162
|
*
|
|
81
163
|
* 2. Closing: asynchronous transition from open to closed. New use should behave as it closed, but may be cleaning up or saving resources asynchronously.
|
|
82
164
|
* Timers and event registrations may still be active, but should be cleaned up by the time the transition to closed completes.
|
|
83
165
|
*
|
|
84
|
-
* 3. Closed: no longer accepts connections from {@link
|
|
166
|
+
* 3. Closed: no longer accepts connections from {@link LocalServiceClient}s, and all containers connected to it are closed.
|
|
85
167
|
* Should have no subscriptions to events or timers which could retain it in memory or trigger asynchronous work.
|
|
86
168
|
* The object can still be used in a limited capacity (typically just to inspect its status (e.g. `isClosed`), and to view (but not edit) the final state of any containers which were connected to it before it closed.)
|
|
87
169
|
*
|
|
@@ -90,75 +172,45 @@ export interface EphemeralServiceOptions extends ServiceOptions {
|
|
|
90
172
|
*
|
|
91
173
|
* @alpha @sealed
|
|
92
174
|
*/
|
|
93
|
-
export interface EphemeralService extends
|
|
94
|
-
/**
|
|
95
|
-
* Close this service, which closes all containers connected to it and releases its resources.
|
|
96
|
-
* @remarks
|
|
97
|
-
* All documents held by this service are discarded, and any timers it (or its containers) were keeping alive
|
|
98
|
-
* are cleaned up.
|
|
99
|
-
* The returned promise resolves once all asynchronous cleanup (including shutting down the in-memory server)
|
|
100
|
-
* has completed.
|
|
101
|
-
* Closing is idempotent: calling it again after the service is closed resolves without doing anything.
|
|
102
|
-
*/
|
|
103
|
-
close(): Promise<void>;
|
|
104
|
-
/**
|
|
105
|
-
* Drives all containers connected to this service toward convergence, processing pending operations and
|
|
106
|
-
* waiting for all dirty containers to save.
|
|
107
|
-
*
|
|
108
|
-
* @param timeoutMilliseconds - The maximum time to wait for containers to quiesce, in milliseconds. Defaults to 30_000.
|
|
109
|
-
*
|
|
110
|
-
* @privateRemarks
|
|
111
|
-
* This is a best-effort implementation simplified from `LoaderContainerTracker.ensureSynchronized`.
|
|
112
|
-
* Currently it does not perform receiver-side sequence-number quiescence or wait for join/leave (audience) ops.
|
|
113
|
-
* See `LoaderContainerTracker.ensureSynchronized` for the fuller version this is based on.
|
|
114
|
-
* For the currently exposed API surface, this should be sufficient,
|
|
115
|
-
* but users down casting to internal types might run into some limitations.
|
|
116
|
-
*/
|
|
117
|
-
synchronize(timeoutMilliseconds?: number): Promise<void>;
|
|
175
|
+
export interface EphemeralService extends LocalService<LocalServiceClient<EphemeralService>> {
|
|
118
176
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* @param options - Options for the client. `oldestSupportedClient` may be omitted because all
|
|
122
|
-
* clients are in the same process, so it defaults to the current version. `service` may be omitted to allocate a new
|
|
123
|
-
* {@link EphemeralService} dedicated to this client, or provided to connect the client to an existing service instance.
|
|
177
|
+
* Closes all containers connected to this service and releases its active resources.
|
|
124
178
|
*
|
|
125
179
|
* @remarks
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* A document created and attached (obtaining an `id`) can be loaded by `id` for as long as its service remains open,
|
|
129
|
-
* even after every container for it has been closed.
|
|
130
|
-
* Closing the service (via {@link EphemeralService.close} or {@link cleanupEphemeralService}) discards all of its
|
|
131
|
-
* documents and releases its resources; afterwards those `id`s can no longer be loaded.
|
|
132
|
-
*
|
|
133
|
-
* When no `service` is provided, a new one is allocated for this client (accessible via {@link EphemeralServiceClient.service}).
|
|
134
|
-
* Provide the same {@link EphemeralService} to multiple clients (via `options.service`) to have them collaborate on the
|
|
135
|
-
* same documents, and control that service's lifetime explicitly.
|
|
136
|
-
*
|
|
137
|
-
* Since a service holds timers while open, tests should close the services they use (e.g. via
|
|
138
|
-
* {@link cleanupEphemeralService} in an `afterEach`) to avoid lingering timers that can hang test runners.
|
|
139
|
-
*
|
|
140
|
-
* @privateRemarks
|
|
141
|
-
* TODO: We should provide a way to extract (for potential serialization as test data) and load documents into a service.
|
|
142
|
-
* This is needed to use this API surface for testing reference documents.
|
|
143
|
-
* Ideally we would provide a service agnostic way to do the export, but likely only support loading them into the local service.
|
|
144
|
-
* This can be done via an API on FluidContainer (or a free function taking one) to do the export, then adding a
|
|
145
|
-
* service specific API (on {@link EphemeralService}) to load from the export format and return the ID of the loaded document.
|
|
146
|
-
*/
|
|
147
|
-
newClient(options: ServiceOptions): EphemeralServiceClient;
|
|
148
|
-
/**
|
|
149
|
-
* A client connected to this service using the default options.
|
|
180
|
+
* Closing is idempotent. Closing an ephemeral service permanently discards all documents it holds,
|
|
181
|
+
* so their IDs can no longer be loaded.
|
|
150
182
|
*/
|
|
151
|
-
|
|
183
|
+
close(): Promise<void>;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A browser-local Fluid service that persists documents in session storage.
|
|
187
|
+
*
|
|
188
|
+
* @remarks
|
|
189
|
+
* Its attached documents remain available after a page reload within the same browser tab. The service is
|
|
190
|
+
* shared within the current JavaScript realm and intentionally has no close operation: its active resources
|
|
191
|
+
* live until the realm is unloaded.
|
|
192
|
+
*
|
|
193
|
+
* Session storage is shared more broadly than JavaScript module state. Separate same-origin realms, such as
|
|
194
|
+
* same-origin frames, or applications that load separate copies of this package can access the same stored
|
|
195
|
+
* documents while running independent local servers. Concurrently editing the same document from such realms
|
|
196
|
+
* is unsupported and may produce inconsistent stored state.
|
|
197
|
+
*
|
|
198
|
+
* Create one with {@link getSessionService}.
|
|
199
|
+
* @alpha @sealed
|
|
200
|
+
*/
|
|
201
|
+
export interface SessionService extends LocalService<LocalServiceClient<SessionService>> {
|
|
152
202
|
}
|
|
153
203
|
/**
|
|
154
|
-
* A {@link @fluidframework/driver-definitions#ServiceClient} connected to a specific {@link
|
|
204
|
+
* A {@link @fluidframework/driver-definitions#ServiceClient} connected to a specific {@link LocalService}.
|
|
205
|
+
*
|
|
206
|
+
* @typeParam TService - The type of local service this client is connected to.
|
|
155
207
|
* @alpha @sealed
|
|
156
208
|
*/
|
|
157
|
-
export interface
|
|
209
|
+
export interface LocalServiceClient<out TService extends LocalService<ServiceClient> = LocalService<ServiceClient>> extends ServiceClient {
|
|
158
210
|
/**
|
|
159
211
|
* The service instance this client is connected to.
|
|
160
212
|
*/
|
|
161
|
-
readonly service:
|
|
213
|
+
readonly service: TService;
|
|
162
214
|
}
|
|
163
215
|
/**
|
|
164
216
|
* A Fluid container backed by an ephemeral (in-memory) local service, implementing
|
|
@@ -170,10 +222,10 @@ export interface EphemeralServiceClient extends ServiceClient {
|
|
|
170
222
|
*
|
|
171
223
|
* @internal
|
|
172
224
|
*/
|
|
173
|
-
export declare class EphemeralServiceContainer<TData> extends ServiceContainerBase<TData,
|
|
174
|
-
readonly service:
|
|
175
|
-
static createDetached<T>(registry: DataStoreRegistry<T>, options:
|
|
176
|
-
static load<T>(registry: DataStoreRegistry<T>, options:
|
|
225
|
+
export declare class EphemeralServiceContainer<TData> extends ServiceContainerBase<TData, LocalServiceOptions> implements FluidContainerWithService<TData> {
|
|
226
|
+
readonly service: LocalService;
|
|
227
|
+
static createDetached<T>(registry: DataStoreRegistry<T>, options: LocalServiceOptions, root: DataStoreKind<T>): Promise<EphemeralServiceContainer<T>>;
|
|
228
|
+
static load<T>(registry: DataStoreRegistry<T>, options: LocalServiceOptions, id: string): Promise<EphemeralServiceContainer<T> & FluidContainerAttached<T>>;
|
|
177
229
|
private constructor();
|
|
178
230
|
close(): void;
|
|
179
231
|
protected createAttachRequest(): IRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ephemeralService.d.ts","sourceRoot":"","sources":["../src/ephemeralService.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAEN,KAAK,cAAc,EACnB,MAAM,0CAA0C,CAAC;AAElD,OAAO,EAEN,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,EAC3B,KAAK,yBAAyB,
|
|
1
|
+
{"version":3,"file":"ephemeralService.d.ts","sourceRoot":"","sources":["../src/ephemeralService.ts"],"names":[],"mappings":"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"}
|
package/lib/ephemeralService.js
CHANGED
|
@@ -11,8 +11,10 @@ import { featureVersion, } from "@fluidframework/driver-definitions/internal";
|
|
|
11
11
|
import { makeCodeLoader, rootDataStoreId, ServiceClientImplementation, ServiceContainerBase, } from "@fluidframework/runtime-utils/internal";
|
|
12
12
|
import { LocalDeltaConnectionServer, } from "@fluidframework/server-local-server";
|
|
13
13
|
import { UsageError } from "@fluidframework/driver-utils/internal";
|
|
14
|
+
import { v4 as uuid } from "uuid";
|
|
14
15
|
import { LocalDocumentServiceFactory } from "./localDocumentServiceFactory.js";
|
|
15
16
|
import { createLocalResolverCreateNewRequest, LocalResolver } from "./localResolver.js";
|
|
17
|
+
import { LocalSessionStorageDbFactory } from "./localSessionStorageDb.js";
|
|
16
18
|
import { pkgVersion } from "./packageVersion.js";
|
|
17
19
|
/**
|
|
18
20
|
* Starts and returns a new {@link EphemeralService}.
|
|
@@ -29,12 +31,34 @@ export function startEphemeralService(isDefault = true) {
|
|
|
29
31
|
if (isDefault && defaultEphemeralService) {
|
|
30
32
|
throw new UsageError("A default EphemeralService is already running");
|
|
31
33
|
}
|
|
32
|
-
const service = new
|
|
34
|
+
const service = new LocalServiceImplementation();
|
|
33
35
|
if (isDefault) {
|
|
34
36
|
defaultEphemeralService = service;
|
|
35
37
|
}
|
|
36
38
|
return service;
|
|
37
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Gets the session-storage-backed local Fluid service for the current JavaScript realm, creating it on first use.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* Attached documents remain available after a page reload within the same browser tab. Repeated calls return
|
|
45
|
+
* the same service instance within the current JavaScript realm.
|
|
46
|
+
*
|
|
47
|
+
* This service is only available in browser environments that provide `sessionStorage`.
|
|
48
|
+
* @returns A local service backed by browser session storage.
|
|
49
|
+
* @alpha
|
|
50
|
+
*/
|
|
51
|
+
export function getSessionService() {
|
|
52
|
+
if (typeof sessionStorage === "undefined") {
|
|
53
|
+
throw new UsageError("SessionService requires browser session storage");
|
|
54
|
+
}
|
|
55
|
+
return (sessionService ??= new LocalServiceImplementation(new LocalSessionStorageDbFactory()));
|
|
56
|
+
}
|
|
57
|
+
/** Closes and clears the current session service for test isolation. */
|
|
58
|
+
export async function resetSessionServiceForTesting() {
|
|
59
|
+
await sessionService?.close();
|
|
60
|
+
sessionService = undefined;
|
|
61
|
+
}
|
|
38
62
|
/**
|
|
39
63
|
* Cleans up the service passed in {@link startEphemeralService}, or the {@link getDefaultEphemeralService|default} if none is passed.
|
|
40
64
|
* @remarks
|
|
@@ -69,31 +93,59 @@ export function getDefaultEphemeralService() {
|
|
|
69
93
|
*/
|
|
70
94
|
let defaultEphemeralService;
|
|
71
95
|
/**
|
|
72
|
-
* The
|
|
96
|
+
* The lazily created session service for this JavaScript realm.
|
|
97
|
+
*/
|
|
98
|
+
let sessionService;
|
|
99
|
+
/**
|
|
100
|
+
* The concrete implementation of local services.
|
|
73
101
|
* @remarks
|
|
74
102
|
* Kept module-private so its mutable state and internal helpers are not part of the public API.
|
|
75
|
-
* Narrow
|
|
103
|
+
* Narrow a {@link LocalService} to it with `LocalServiceImplementation.narrow`.
|
|
76
104
|
*/
|
|
77
|
-
class
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
105
|
+
class LocalServiceImplementation extends ErasedTypeImplementation {
|
|
106
|
+
/**
|
|
107
|
+
* The active in-memory server shared by this service's containers so they can communicate.
|
|
108
|
+
* Replaced after document maintenance resets the server.
|
|
109
|
+
*/
|
|
110
|
+
server;
|
|
111
|
+
/** The document service factory bound to the active {@link LocalServiceImplementation.server}. */
|
|
112
|
+
documentServiceFactory;
|
|
113
|
+
/** The persistent database factory retained when the active server is replaced. */
|
|
114
|
+
databaseFactory;
|
|
115
|
+
/** The open containers connected to this service. */
|
|
83
116
|
containers = new Set();
|
|
117
|
+
/** Whether this service has been permanently closed. */
|
|
84
118
|
closed = false;
|
|
85
|
-
|
|
119
|
+
/** Whether document deletion is temporarily preventing other service operations. */
|
|
120
|
+
maintenanceInProgress = false;
|
|
121
|
+
constructor(databaseFactory) {
|
|
86
122
|
super();
|
|
123
|
+
this.server = LocalDeltaConnectionServer.create(databaseFactory);
|
|
124
|
+
this.databaseFactory = this.server.testDbFactory;
|
|
125
|
+
this.documentServiceFactory = new LocalDocumentServiceFactory(this.server);
|
|
87
126
|
this.defaultClient = this.newClient();
|
|
88
127
|
}
|
|
89
128
|
newClient(options) {
|
|
90
129
|
const finalOptions = {
|
|
130
|
+
...options,
|
|
91
131
|
oldestSupportedClient: options?.oldestSupportedClient ?? featureVersion(pkgVersion),
|
|
92
132
|
service: this,
|
|
93
133
|
};
|
|
94
|
-
return new
|
|
134
|
+
return new LocalServiceClientImplementation(finalOptions);
|
|
95
135
|
}
|
|
96
136
|
defaultClient;
|
|
137
|
+
async listDocumentIds() {
|
|
138
|
+
this.ensureAvailable();
|
|
139
|
+
const documentCollection = await this.server.databaseManager.getDocumentCollection();
|
|
140
|
+
const documents = await documentCollection.findAll();
|
|
141
|
+
return documents.map((document) => document.documentId);
|
|
142
|
+
}
|
|
143
|
+
async deleteDocument(id) {
|
|
144
|
+
await this.deleteDocuments(id);
|
|
145
|
+
}
|
|
146
|
+
async deleteAllDocuments() {
|
|
147
|
+
await this.deleteDocuments();
|
|
148
|
+
}
|
|
97
149
|
async close() {
|
|
98
150
|
if (this.closed) {
|
|
99
151
|
return;
|
|
@@ -110,6 +162,57 @@ class EphemeralServiceImplementation extends ErasedTypeImplementation {
|
|
|
110
162
|
// server rather than any container, so closing containers alone would leave them running.
|
|
111
163
|
await this.server.close();
|
|
112
164
|
}
|
|
165
|
+
ensureAvailable() {
|
|
166
|
+
if (this.closed) {
|
|
167
|
+
throw new UsageError("Local service is closed");
|
|
168
|
+
}
|
|
169
|
+
if (this.maintenanceInProgress) {
|
|
170
|
+
throw new UsageError("Local service document maintenance is already in progress");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Delete all documents unless `id` is specified, in which case only that document is deleted.
|
|
175
|
+
*/
|
|
176
|
+
async deleteDocuments(id) {
|
|
177
|
+
this.ensureAvailable();
|
|
178
|
+
if (this.containers.size > 0) {
|
|
179
|
+
throw new UsageError("Close all containers before deleting local service documents");
|
|
180
|
+
}
|
|
181
|
+
this.maintenanceInProgress = true;
|
|
182
|
+
let serverClosed = false;
|
|
183
|
+
try {
|
|
184
|
+
await this.server.close();
|
|
185
|
+
serverClosed = true;
|
|
186
|
+
const databaseManager = this.server.databaseManager;
|
|
187
|
+
const filter = id === undefined ? {} : { documentId: id };
|
|
188
|
+
const historianDatabase = this.databaseFactory.testDatabase;
|
|
189
|
+
const documentCollection = await databaseManager.getDocumentCollection();
|
|
190
|
+
const checkpointCollection = await databaseManager.getCheckpointCollection();
|
|
191
|
+
const deltaCollection = await databaseManager.getDeltaCollection(undefined, id);
|
|
192
|
+
const scribeDeltaCollection = await databaseManager.getScribeDeltaCollection(undefined, id);
|
|
193
|
+
const deletions = [
|
|
194
|
+
documentCollection.deleteMany(filter),
|
|
195
|
+
checkpointCollection.deleteMany(filter),
|
|
196
|
+
deltaCollection.deleteMany(filter),
|
|
197
|
+
scribeDeltaCollection.deleteMany(filter),
|
|
198
|
+
historianDatabase
|
|
199
|
+
.collection("refs")
|
|
200
|
+
.deleteMany(id === undefined ? {} : { _id: `heads/${id}` }),
|
|
201
|
+
];
|
|
202
|
+
if (id === undefined) {
|
|
203
|
+
const nodeCollection = await databaseManager.getNodeCollection();
|
|
204
|
+
deletions.push(nodeCollection.deleteMany({}), historianDatabase.collection("blobs").deleteMany({}), historianDatabase.collection("commits").deleteMany({}), historianDatabase.collection("trees").deleteMany({}));
|
|
205
|
+
}
|
|
206
|
+
await Promise.all(deletions);
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
if (serverClosed) {
|
|
210
|
+
this.server = LocalDeltaConnectionServer.create(this.databaseFactory);
|
|
211
|
+
this.documentServiceFactory = new LocalDocumentServiceFactory(this.server);
|
|
212
|
+
}
|
|
213
|
+
this.maintenanceInProgress = false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
113
216
|
async synchronize(timeoutMilliseconds = 30_000) {
|
|
114
217
|
// Timeout to allow for better errors in the case of hangs.
|
|
115
218
|
let timedOut = false;
|
|
@@ -182,6 +285,7 @@ class EphemeralServiceImplementation extends ErasedTypeImplementation {
|
|
|
182
285
|
* @remarks Internal helper for {@link EphemeralServiceContainer}; not part of the public {@link EphemeralService} API.
|
|
183
286
|
*/
|
|
184
287
|
getDocumentServiceFactory() {
|
|
288
|
+
this.ensureAvailable();
|
|
185
289
|
assert(!this.closed, 0xd11 /* Cannot create or load containers on a closed EphemeralService */);
|
|
186
290
|
return this.documentServiceFactory;
|
|
187
291
|
}
|
|
@@ -200,7 +304,7 @@ class EphemeralServiceImplementation extends ErasedTypeImplementation {
|
|
|
200
304
|
this.containers.delete(container);
|
|
201
305
|
}
|
|
202
306
|
}
|
|
203
|
-
class
|
|
307
|
+
class LocalServiceClientImplementation extends ServiceClientImplementation {
|
|
204
308
|
service;
|
|
205
309
|
constructor(options) {
|
|
206
310
|
super(options, EphemeralServiceContainer);
|
|
@@ -236,7 +340,6 @@ const urlResolver = new LocalResolver();
|
|
|
236
340
|
const createLoadExistingRequest = (documentId) => {
|
|
237
341
|
return { url: `http://localhost:3000/${documentId}` };
|
|
238
342
|
};
|
|
239
|
-
let documentIdCounter = 0;
|
|
240
343
|
/**
|
|
241
344
|
* A Fluid container backed by an ephemeral (in-memory) local service, implementing
|
|
242
345
|
* {@link @fluidframework/driver-definitions#FluidContainerWithService}.
|
|
@@ -250,9 +353,10 @@ let documentIdCounter = 0;
|
|
|
250
353
|
export class EphemeralServiceContainer extends ServiceContainerBase {
|
|
251
354
|
service;
|
|
252
355
|
static async createDetached(registry, options, root) {
|
|
253
|
-
|
|
356
|
+
LocalServiceImplementation.narrow(options.service);
|
|
254
357
|
const container = await createDetachedContainer({
|
|
255
358
|
codeDetails: { package: "1.0" },
|
|
359
|
+
logger: options.logger,
|
|
256
360
|
urlResolver,
|
|
257
361
|
documentServiceFactory: options.service.getDocumentServiceFactory(),
|
|
258
362
|
codeLoader: makeCodeLoader(registry, options.oldestSupportedClient, containerRuntimeLoader, root),
|
|
@@ -260,9 +364,10 @@ export class EphemeralServiceContainer extends ServiceContainerBase {
|
|
|
260
364
|
return new EphemeralServiceContainer(registry, options, container, (await container.getEntryPoint()), undefined);
|
|
261
365
|
}
|
|
262
366
|
static async load(registry, options, id) {
|
|
263
|
-
|
|
367
|
+
LocalServiceImplementation.narrow(options.service);
|
|
264
368
|
const containerInner = await loadExistingContainer({
|
|
265
369
|
request: createLoadExistingRequest(id),
|
|
370
|
+
logger: options.logger,
|
|
266
371
|
urlResolver,
|
|
267
372
|
documentServiceFactory: options.service.getDocumentServiceFactory(),
|
|
268
373
|
codeLoader: makeCodeLoader(registry, options.oldestSupportedClient, containerRuntimeLoader),
|
|
@@ -274,18 +379,17 @@ export class EphemeralServiceContainer extends ServiceContainerBase {
|
|
|
274
379
|
constructor(registry, options, container, data, id) {
|
|
275
380
|
super(registry, options, container, data, id);
|
|
276
381
|
this.service = options.service;
|
|
277
|
-
|
|
382
|
+
LocalServiceImplementation.narrow(this.service);
|
|
278
383
|
this.service.addContainer(this);
|
|
279
384
|
}
|
|
280
385
|
close() {
|
|
281
386
|
super.close();
|
|
282
387
|
// Remove this now-closed container from its service's set of open containers.
|
|
283
|
-
|
|
388
|
+
LocalServiceImplementation.narrow(this.service);
|
|
284
389
|
this.service.removeContainer(this);
|
|
285
390
|
}
|
|
286
391
|
createAttachRequest() {
|
|
287
|
-
|
|
288
|
-
return createLocalResolverCreateNewRequest(documentId);
|
|
392
|
+
return createLocalResolverCreateNewRequest(uuid());
|
|
289
393
|
}
|
|
290
394
|
}
|
|
291
395
|
//# sourceMappingURL=ephemeralService.js.map
|