@fluidframework/local-driver 2.113.1 → 2.114.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 +69 -0
- package/README.md +2 -0
- package/alpha.d.ts +11 -0
- package/api-extractor/api-extractor-lint-alpha.cjs.json +5 -0
- package/api-extractor/api-extractor-lint-alpha.esm.json +5 -0
- package/api-extractor/api-extractor.current.json +4 -1
- package/api-report/local-driver.alpha.api.md +31 -0
- package/dist/alpha.d.ts +19 -0
- package/dist/ephemeralService.d.ts +177 -0
- package/dist/ephemeralService.d.ts.map +1 -0
- package/dist/ephemeralService.js +295 -0
- package/dist/ephemeralService.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.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/eslint.config.mts +0 -11
- package/lib/alpha.d.ts +19 -0
- package/lib/ephemeralService.d.ts +177 -0
- package/lib/ephemeralService.d.ts.map +1 -0
- package/lib/ephemeralService.js +288 -0
- package/lib/ephemeralService.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.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 +33 -18
- package/src/ephemeralService.ts +551 -0
- package/src/index.ts +10 -0
- package/src/packageVersion.ts +1 -1
- package/tsconfig.json +3 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
ConnectionState,
|
|
8
|
+
type IContainer,
|
|
9
|
+
} from "@fluidframework/container-definitions/internal";
|
|
10
|
+
import {
|
|
11
|
+
createDetachedContainer,
|
|
12
|
+
loadExistingContainer,
|
|
13
|
+
} from "@fluidframework/container-loader/internal";
|
|
14
|
+
import { ContainerRuntime } from "@fluidframework/container-runtime/internal";
|
|
15
|
+
import type { IRequest } from "@fluidframework/core-interfaces";
|
|
16
|
+
import {
|
|
17
|
+
ErasedTypeImplementation,
|
|
18
|
+
type ErasedBaseType,
|
|
19
|
+
} from "@fluidframework/core-interfaces/internal";
|
|
20
|
+
import { assert } from "@fluidframework/core-utils/internal";
|
|
21
|
+
import type {
|
|
22
|
+
DataStoreKind,
|
|
23
|
+
DataStoreRegistry,
|
|
24
|
+
FluidContainerAttached,
|
|
25
|
+
FluidContainerWithService,
|
|
26
|
+
Registry,
|
|
27
|
+
ServiceClient,
|
|
28
|
+
ServiceOptions,
|
|
29
|
+
} from "@fluidframework/driver-definitions/internal";
|
|
30
|
+
import { featureVersion } from "@fluidframework/driver-definitions/internal";
|
|
31
|
+
import {
|
|
32
|
+
type ContainerRuntimeLoader,
|
|
33
|
+
type ContainerRuntimeLoaderParams,
|
|
34
|
+
makeCodeLoader,
|
|
35
|
+
rootDataStoreId,
|
|
36
|
+
ServiceClientImplementation,
|
|
37
|
+
ServiceContainerBase,
|
|
38
|
+
} from "@fluidframework/runtime-utils/internal";
|
|
39
|
+
import {
|
|
40
|
+
LocalDeltaConnectionServer,
|
|
41
|
+
type ILocalDeltaConnectionServer,
|
|
42
|
+
} from "@fluidframework/server-local-server";
|
|
43
|
+
import { UsageError } from "@fluidframework/driver-utils/internal";
|
|
44
|
+
|
|
45
|
+
import { LocalDocumentServiceFactory } from "./localDocumentServiceFactory.js";
|
|
46
|
+
import { createLocalResolverCreateNewRequest, LocalResolver } from "./localResolver.js";
|
|
47
|
+
import { pkgVersion } from "./packageVersion.js";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Starts and returns a new {@link EphemeralService}.
|
|
51
|
+
* @param isDefault - Whether this service should saved as the default service for {@link cleanupEphemeralService} to cleanup.
|
|
52
|
+
* Defaults to true.
|
|
53
|
+
* @remarks
|
|
54
|
+
* The returned service owns an in-memory server and holds the documents created through clients connected to it.
|
|
55
|
+
* {@link cleanupEphemeralService} can be used to ensure the service is properly cleaned up (a no-op if stopped/closed already).
|
|
56
|
+
*
|
|
57
|
+
* As a service, it may start timers which may require an explicit `close` to fully free.
|
|
58
|
+
* @alpha
|
|
59
|
+
*/
|
|
60
|
+
export function startEphemeralService(isDefault = true): EphemeralService {
|
|
61
|
+
if (isDefault && defaultEphemeralService) {
|
|
62
|
+
throw new UsageError("A default EphemeralService is already running");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const service = new EphemeralServiceImplementation();
|
|
66
|
+
if (isDefault) {
|
|
67
|
+
defaultEphemeralService = service;
|
|
68
|
+
}
|
|
69
|
+
return service;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Cleans up the service passed in {@link startEphemeralService}, or the {@link getDefaultEphemeralService|default} if none is passed.
|
|
74
|
+
* @remarks
|
|
75
|
+
* This closes the service, and all its containers.
|
|
76
|
+
* This is a good way to ensure the service and its containers leave no lingering timers
|
|
77
|
+
* which could leak memory, trigger asynchronous work or prevent a clean process exit.
|
|
78
|
+
* @alpha
|
|
79
|
+
*/
|
|
80
|
+
export async function cleanupEphemeralService(service?: EphemeralService): Promise<void> {
|
|
81
|
+
const toCleanup = service ?? defaultEphemeralService;
|
|
82
|
+
if (toCleanup) {
|
|
83
|
+
// TODO: we may want to make closing of containers a separate operation which is done here.
|
|
84
|
+
await toCleanup.close();
|
|
85
|
+
}
|
|
86
|
+
if (toCleanup === defaultEphemeralService) {
|
|
87
|
+
defaultEphemeralService = undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get the default {@link EphemeralService} if one has been {@link startEphemeralService|started}.
|
|
93
|
+
* @throws If no default service is running.
|
|
94
|
+
* @alpha
|
|
95
|
+
*/
|
|
96
|
+
export function getDefaultEphemeralService(): EphemeralService {
|
|
97
|
+
if (defaultEphemeralService) {
|
|
98
|
+
return defaultEphemeralService;
|
|
99
|
+
}
|
|
100
|
+
throw new UsageError("No default EphemeralService is running");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Internal Options for creating an {@link EphemeralServiceClient}, extending {@link @fluidframework/driver-definitions#ServiceOptions}
|
|
105
|
+
* with the {@link EphemeralService} the client should connect to.
|
|
106
|
+
* @input
|
|
107
|
+
* @internal
|
|
108
|
+
*/
|
|
109
|
+
export interface EphemeralServiceOptions extends ServiceOptions {
|
|
110
|
+
/**
|
|
111
|
+
* The service instance to connect to.
|
|
112
|
+
*/
|
|
113
|
+
readonly service: EphemeralService;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* An in-memory Fluid service that can produce connected {@link EphemeralServiceClient}s.
|
|
118
|
+
* @remarks
|
|
119
|
+
* All documents created through clients connected to a given `EphemeralService` are held in-memory by that service.
|
|
120
|
+
* Closing the service (via {@link EphemeralService.close} or {@link cleanupEphemeralService}) closes the connections
|
|
121
|
+
* to any remaining open containers, and cleans up the service's timers.
|
|
122
|
+
*
|
|
123
|
+
* Create one with {@link startEphemeralService}.
|
|
124
|
+
*
|
|
125
|
+
* Most {@link @fluidframework/driver-definitions#ServiceClient} implementations would take in a URL and credentials to connect to a service,
|
|
126
|
+
* but that is not needed for the ephemeral in-memory service.
|
|
127
|
+
* Instead this object representing the actual service instance is provided.
|
|
128
|
+
* @privateRemarks
|
|
129
|
+
* This is separated out from the actual {@link @fluidframework/driver-definitions#ServiceClient} object so that it's possible to create multiple service clients
|
|
130
|
+
* connected to the same service.
|
|
131
|
+
* Doing so is rarely necessary, but would be needed to test multiple clients collaborating on the same
|
|
132
|
+
* document with different minVersionForCollaboration values.
|
|
133
|
+
* This also exposes a place to put APIs for preloading and exporting document contents in the future.
|
|
134
|
+
*
|
|
135
|
+
* This is an erased type: its only implementation is the module-private {@link EphemeralServiceImplementation}, which holds
|
|
136
|
+
* the mutable server and container state so it does not appear on this public type.
|
|
137
|
+
*
|
|
138
|
+
* TODO: formalize this lifecycle with an interface which documents these stages.
|
|
139
|
+
* Lifecycle:
|
|
140
|
+
* The intended lifecycle of an {@link EphemeralService} follows roughly the same pattern as containers:
|
|
141
|
+
*
|
|
142
|
+
* 1. Open: accepts connections from {@link EphemeralServiceClient}s, which can create and load containers.
|
|
143
|
+
* Might have timers and event registrations which can trigger asynchronous work, and retain the object in memory.
|
|
144
|
+
*
|
|
145
|
+
* 2. Closing: asynchronous transition from open to closed. New use should behave as it closed, but may be cleaning up or saving resources asynchronously.
|
|
146
|
+
* Timers and event registrations may still be active, but should be cleaned up by the time the transition to closed completes.
|
|
147
|
+
*
|
|
148
|
+
* 3. Closed: no longer accepts connections from {@link EphemeralServiceClient}s, and all containers connected to it are closed.
|
|
149
|
+
* Should have no subscriptions to events or timers which could retain it in memory or trigger asynchronous work.
|
|
150
|
+
* 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.)
|
|
151
|
+
*
|
|
152
|
+
* Events or errors can cause an open to closing transition. Any nonfunctional state, including error states, should be considered as closed (or closing which will transition to closed),
|
|
153
|
+
* and meet the requirements of closed with regards to timers and events.
|
|
154
|
+
*
|
|
155
|
+
* @alpha @sealed
|
|
156
|
+
*/
|
|
157
|
+
export interface EphemeralService extends ErasedBaseType<readonly ["EphemeralService"]> {
|
|
158
|
+
/**
|
|
159
|
+
* Close this service, which closes all containers connected to it and releases its resources.
|
|
160
|
+
* @remarks
|
|
161
|
+
* All documents held by this service are discarded, and any timers it (or its containers) were keeping alive
|
|
162
|
+
* are cleaned up.
|
|
163
|
+
* The returned promise resolves once all asynchronous cleanup (including shutting down the in-memory server)
|
|
164
|
+
* has completed.
|
|
165
|
+
* Closing is idempotent: calling it again after the service is closed resolves without doing anything.
|
|
166
|
+
*/
|
|
167
|
+
close(): Promise<void>;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Drives all containers connected to this service toward convergence, processing pending operations and
|
|
171
|
+
* waiting for all dirty containers to save.
|
|
172
|
+
*
|
|
173
|
+
* @param timeoutMilliseconds - The maximum time to wait for containers to quiesce, in milliseconds. Defaults to 30_000.
|
|
174
|
+
*
|
|
175
|
+
* @privateRemarks
|
|
176
|
+
* This is a best-effort implementation simplified from `LoaderContainerTracker.ensureSynchronized`.
|
|
177
|
+
* Currently it does not perform receiver-side sequence-number quiescence or wait for join/leave (audience) ops.
|
|
178
|
+
* See `LoaderContainerTracker.ensureSynchronized` for the fuller version this is based on.
|
|
179
|
+
* For the currently exposed API surface, this should be sufficient,
|
|
180
|
+
* but users down casting to internal types might run into some limitations.
|
|
181
|
+
*/
|
|
182
|
+
synchronize(timeoutMilliseconds?: number): Promise<void>;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Creates and returns a {@link EphemeralServiceClient} for an in-memory, ephemeral Fluid service.
|
|
186
|
+
*
|
|
187
|
+
* @param options - Options for the client. `minVersionForCollaboration` may be omitted (since all collaborators
|
|
188
|
+
* are in the same process, it defaults to the current version). `service` may be omitted to allocate a new
|
|
189
|
+
* {@link EphemeralService} dedicated to this client, or provided to connect the client to an existing service instance.
|
|
190
|
+
*
|
|
191
|
+
* @remarks
|
|
192
|
+
* The service is ephemeral and in-memory: all documents are held by the {@link EphemeralService} the client is
|
|
193
|
+
* connected to, and live for as long as that service is open — independent of whether any container for them is open.
|
|
194
|
+
* A document created and attached (obtaining an `id`) can be loaded by `id` for as long as its service remains open,
|
|
195
|
+
* even after every container for it has been closed.
|
|
196
|
+
* Closing the service (via {@link EphemeralService.close} or {@link cleanupEphemeralService}) discards all of its
|
|
197
|
+
* documents and releases its resources; afterwards those `id`s can no longer be loaded.
|
|
198
|
+
*
|
|
199
|
+
* When no `service` is provided, a new one is allocated for this client (accessible via {@link EphemeralServiceClient.service}).
|
|
200
|
+
* Provide the same {@link EphemeralService} to multiple clients (via `options.service`) to have them collaborate on the
|
|
201
|
+
* same documents, and control that service's lifetime explicitly.
|
|
202
|
+
*
|
|
203
|
+
* Since a service holds timers while open, tests should close the services they use (e.g. via
|
|
204
|
+
* {@link cleanupEphemeralService} in an `afterEach`) to avoid lingering timers that can hang test runners.
|
|
205
|
+
*
|
|
206
|
+
* @privateRemarks
|
|
207
|
+
* TODO: We should provide a way to extract (for potential serialization as test data) and load documents into a service.
|
|
208
|
+
* This is needed to use this API surface for testing reference documents.
|
|
209
|
+
* Ideally we would provide a service agnostic way to do the export, but likely only support loading them into the local service.
|
|
210
|
+
* This can be done via an API on FluidContainer (or a free function taking one) to do the export, then adding a
|
|
211
|
+
* service specific API (on {@link EphemeralService}) to load from the export format and return the ID of the loaded document.
|
|
212
|
+
*/
|
|
213
|
+
newClient(options: ServiceOptions): EphemeralServiceClient;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A client connected to this service using the default options.
|
|
217
|
+
*/
|
|
218
|
+
readonly defaultClient: EphemeralServiceClient;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The {@link defaultEphemeralService} if one has been {@link startEphemeralService|started}.
|
|
223
|
+
*/
|
|
224
|
+
let defaultEphemeralService: EphemeralServiceImplementation | undefined;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The concrete implementation of {@link EphemeralService}.
|
|
228
|
+
* @remarks
|
|
229
|
+
* Kept module-private so its mutable state and internal helpers are not part of the public API.
|
|
230
|
+
* Narrow an {@link EphemeralService} to it with `EphemeralServiceImplementation.narrow`.
|
|
231
|
+
*/
|
|
232
|
+
class EphemeralServiceImplementation
|
|
233
|
+
extends ErasedTypeImplementation<EphemeralService>
|
|
234
|
+
implements EphemeralService
|
|
235
|
+
{
|
|
236
|
+
// A single server is shared by all containers connected to this service so they can communicate with each other.
|
|
237
|
+
private readonly server: ILocalDeltaConnectionServer =
|
|
238
|
+
LocalDeltaConnectionServer.create(
|
|
239
|
+
// new LocalSessionStorageDbFactory(),
|
|
240
|
+
);
|
|
241
|
+
private readonly documentServiceFactory = new LocalDocumentServiceFactory(this.server);
|
|
242
|
+
private readonly containers = new Set<EphemeralServiceContainer<unknown>>();
|
|
243
|
+
private closed = false;
|
|
244
|
+
|
|
245
|
+
public constructor() {
|
|
246
|
+
super();
|
|
247
|
+
this.defaultClient = this.newClient();
|
|
248
|
+
}
|
|
249
|
+
public newClient(options?: Partial<ServiceOptions>): EphemeralServiceClient {
|
|
250
|
+
const finalOptions: EphemeralServiceOptions = {
|
|
251
|
+
minVersionForCollaboration:
|
|
252
|
+
options?.minVersionForCollaboration ?? featureVersion(pkgVersion),
|
|
253
|
+
service: this,
|
|
254
|
+
};
|
|
255
|
+
return new EphemeralServiceClientImplementation(finalOptions);
|
|
256
|
+
}
|
|
257
|
+
public readonly defaultClient: EphemeralServiceClient;
|
|
258
|
+
|
|
259
|
+
public async close(): Promise<void> {
|
|
260
|
+
if (this.closed) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.closed = true;
|
|
264
|
+
|
|
265
|
+
// Close every open container via the same public close() path a user would use.
|
|
266
|
+
// We might want to remove this.
|
|
267
|
+
const toClose = [...this.containers];
|
|
268
|
+
this.containers.clear();
|
|
269
|
+
for (const c of toClose) {
|
|
270
|
+
c.close();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Shut down the in-memory server. Its timers (e.g. the Deli read-client idle `setInterval`) belong to the
|
|
274
|
+
// server rather than any container, so closing containers alone would leave them running.
|
|
275
|
+
await this.server.close();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public async synchronize(timeoutMilliseconds = 30_000): Promise<void> {
|
|
279
|
+
// Timeout to allow for better errors in the case of hangs.
|
|
280
|
+
let timedOut = false;
|
|
281
|
+
let deadlineTimer: ReturnType<typeof setTimeout> | undefined;
|
|
282
|
+
const deadline = new Promise<true>((resolve) => {
|
|
283
|
+
deadlineTimer = setTimeout(() => {
|
|
284
|
+
timedOut = true;
|
|
285
|
+
resolve(true);
|
|
286
|
+
}, timeoutMilliseconds);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
try {
|
|
290
|
+
// Require two consecutive quiescent passes (no dirty containers and no pending server work),
|
|
291
|
+
// each separated by a macrotask turn, to give late side effects a chance to surface.
|
|
292
|
+
let clean = 0;
|
|
293
|
+
while (clean < 2) {
|
|
294
|
+
if (timedOut) {
|
|
295
|
+
throw new UsageError(
|
|
296
|
+
`EphemeralService.synchronize timed out after ${timeoutMilliseconds}ms waiting for local containers to quiesce.`,
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Yield a macrotask turn *first*, so the local server's scheduled broadcast send and each
|
|
301
|
+
// container's inbound op processing can run before we sample their state below. Sampling
|
|
302
|
+
// hasPendingWork() in a tight `while (await ...)` loop instead would starve that scheduled
|
|
303
|
+
// send (it is a macrotask, while the await resolves on the microtask queue) and could hang.
|
|
304
|
+
await new Promise<void>((resolve) => {
|
|
305
|
+
setTimeout(resolve, 0);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
// Prune any containers that have closed since the last pass.
|
|
309
|
+
for (const container of [...this.containers]) {
|
|
310
|
+
if (container.container.closed) {
|
|
311
|
+
this.containers.delete(container);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const containersToApply = [...this.containers].map((container) => container.container);
|
|
315
|
+
|
|
316
|
+
// Ignore readonly/disconnected dirty containers: they can't send ops, so nothing can be done about them being dirty here.
|
|
317
|
+
// Neither state is reachable through the ephemeral service API today, but the checks are cheap and keep this robust to future changes.
|
|
318
|
+
const dirtyContainers = containersToApply.filter((c) => {
|
|
319
|
+
const { deltaManager, isDirty, connectionState } = c;
|
|
320
|
+
return (
|
|
321
|
+
connectionState !== ConnectionState.Disconnected &&
|
|
322
|
+
deltaManager.readOnlyInfo.readonly !== true &&
|
|
323
|
+
isDirty
|
|
324
|
+
);
|
|
325
|
+
});
|
|
326
|
+
if (dirtyContainers.length > 0) {
|
|
327
|
+
// Bound this wait by the shared deadline: a container that never saves (and never
|
|
328
|
+
// closes) must not block past the overall timeout, since the top-of-loop check can't
|
|
329
|
+
// run while we are awaiting here.
|
|
330
|
+
await Promise.race([
|
|
331
|
+
Promise.all(
|
|
332
|
+
dirtyContainers.map(async (c) =>
|
|
333
|
+
Promise.race([
|
|
334
|
+
new Promise((resolve) => c.once("saved", resolve)),
|
|
335
|
+
new Promise((resolve) => c.once("closed", resolve)),
|
|
336
|
+
]),
|
|
337
|
+
),
|
|
338
|
+
),
|
|
339
|
+
deadline,
|
|
340
|
+
]);
|
|
341
|
+
|
|
342
|
+
clean = 0;
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Sample pending server work once per pass (the macrotask yield above gave the broadcaster's
|
|
347
|
+
// scheduled send a chance to run first).
|
|
348
|
+
if (await Promise.race([this.server.hasPendingWork(), deadline])) {
|
|
349
|
+
clean = 0;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
clean++;
|
|
354
|
+
}
|
|
355
|
+
} finally {
|
|
356
|
+
clearTimeout(deadlineTimer);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* The document service factory for this service.
|
|
362
|
+
* @remarks Internal helper for {@link EphemeralServiceContainer}; not part of the public {@link EphemeralService} API.
|
|
363
|
+
*/
|
|
364
|
+
public getDocumentServiceFactory(): LocalDocumentServiceFactory {
|
|
365
|
+
assert(
|
|
366
|
+
!this.closed,
|
|
367
|
+
0xd11 /* Cannot create or load containers on a closed EphemeralService */,
|
|
368
|
+
);
|
|
369
|
+
return this.documentServiceFactory;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Registers a newly created container as connected to this service.
|
|
374
|
+
* @remarks Internal helper for {@link EphemeralServiceContainer}; not part of the public {@link EphemeralService} API.
|
|
375
|
+
*/
|
|
376
|
+
public addContainer(container: EphemeralServiceContainer<unknown>): void {
|
|
377
|
+
this.containers.add(container);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Removes a now-closed container from this service.
|
|
382
|
+
* @remarks Internal helper for {@link EphemeralServiceContainer}; not part of the public {@link EphemeralService} API.
|
|
383
|
+
*/
|
|
384
|
+
public removeContainer(container: EphemeralServiceContainer<unknown>): void {
|
|
385
|
+
this.containers.delete(container);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* A {@link @fluidframework/driver-definitions#ServiceClient} connected to a specific {@link EphemeralService}.
|
|
391
|
+
* @alpha @sealed
|
|
392
|
+
*/
|
|
393
|
+
export interface EphemeralServiceClient extends ServiceClient {
|
|
394
|
+
/**
|
|
395
|
+
* The service instance this client is connected to.
|
|
396
|
+
*/
|
|
397
|
+
readonly service: EphemeralService;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
class EphemeralServiceClientImplementation
|
|
401
|
+
extends ServiceClientImplementation<EphemeralServiceOptions>
|
|
402
|
+
implements EphemeralServiceClient
|
|
403
|
+
{
|
|
404
|
+
public readonly service: EphemeralService;
|
|
405
|
+
|
|
406
|
+
public constructor(options: EphemeralServiceOptions) {
|
|
407
|
+
super(options, EphemeralServiceContainer);
|
|
408
|
+
this.service = options.service;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const containerRuntimeLoader: ContainerRuntimeLoader = async (
|
|
413
|
+
parameters: ContainerRuntimeLoaderParams,
|
|
414
|
+
) => {
|
|
415
|
+
const { runtime } = await ContainerRuntime.loadRuntime2({
|
|
416
|
+
context: parameters.context,
|
|
417
|
+
registry: parameters.registry,
|
|
418
|
+
provideEntryPoint: parameters.provideEntryPoint,
|
|
419
|
+
existing: parameters.existing,
|
|
420
|
+
minVersionForCollab: parameters.minVersionForCollab,
|
|
421
|
+
runtimeOptions: { enableRuntimeIdCompressor: "on" },
|
|
422
|
+
});
|
|
423
|
+
if (!parameters.existing) {
|
|
424
|
+
assert(
|
|
425
|
+
parameters.newContainerRootType !== undefined,
|
|
426
|
+
0xd12 /* Root data store kind must be provided for new containers */,
|
|
427
|
+
);
|
|
428
|
+
const dataStore = await runtime.createDataStore(parameters.newContainerRootType);
|
|
429
|
+
const aliasResult = await dataStore.trySetAlias(rootDataStoreId);
|
|
430
|
+
assert(
|
|
431
|
+
aliasResult === "Success",
|
|
432
|
+
0xd13 /* Should be able to set alias on new data store */,
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
return runtime;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const urlResolver = new LocalResolver();
|
|
439
|
+
/**
|
|
440
|
+
* Create a request to open an existing document.
|
|
441
|
+
*
|
|
442
|
+
* @param documentId - the existing document to open.
|
|
443
|
+
* @privateRemarks
|
|
444
|
+
* Like createLocalResolverCreateNewRequest, but without the option to create a new document.
|
|
445
|
+
* TODO: At some point we should avoid specifying the URL in so many places, but the current APIs don't accommodate it yet.
|
|
446
|
+
*/
|
|
447
|
+
const createLoadExistingRequest = (documentId: string): IRequest => {
|
|
448
|
+
return { url: `http://localhost:3000/${documentId}` };
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
let documentIdCounter = 0;
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* A Fluid container backed by an ephemeral (in-memory) local service, implementing
|
|
455
|
+
* {@link @fluidframework/driver-definitions#FluidContainerWithService}.
|
|
456
|
+
*
|
|
457
|
+
* @remarks
|
|
458
|
+
* Data is stored in-memory by the {@link EphemeralService} the container's client is connected to (see
|
|
459
|
+
* {@link EphemeralServiceContainer.service}), enabling side-by-side collaboration testing without a real server.
|
|
460
|
+
*
|
|
461
|
+
* @internal
|
|
462
|
+
*/
|
|
463
|
+
export class EphemeralServiceContainer<TData>
|
|
464
|
+
extends ServiceContainerBase<TData, EphemeralServiceOptions>
|
|
465
|
+
implements FluidContainerWithService<TData>
|
|
466
|
+
{
|
|
467
|
+
public readonly service: EphemeralService;
|
|
468
|
+
|
|
469
|
+
public static async createDetached<T>(
|
|
470
|
+
registry: DataStoreRegistry<T>,
|
|
471
|
+
options: EphemeralServiceOptions,
|
|
472
|
+
root: DataStoreKind<T>,
|
|
473
|
+
): Promise<EphemeralServiceContainer<T>> {
|
|
474
|
+
EphemeralServiceImplementation.narrow(options.service);
|
|
475
|
+
const container: IContainer = await createDetachedContainer({
|
|
476
|
+
codeDetails: { package: "1.0" },
|
|
477
|
+
urlResolver,
|
|
478
|
+
documentServiceFactory: options.service.getDocumentServiceFactory(),
|
|
479
|
+
codeLoader: makeCodeLoader(
|
|
480
|
+
registry,
|
|
481
|
+
options.minVersionForCollaboration,
|
|
482
|
+
containerRuntimeLoader,
|
|
483
|
+
root,
|
|
484
|
+
),
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
return new EphemeralServiceContainer<T>(
|
|
488
|
+
registry,
|
|
489
|
+
options,
|
|
490
|
+
container,
|
|
491
|
+
(await container.getEntryPoint()) as T,
|
|
492
|
+
undefined,
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
public static async load<T>(
|
|
497
|
+
registry: DataStoreRegistry<T>,
|
|
498
|
+
options: EphemeralServiceOptions,
|
|
499
|
+
id: string,
|
|
500
|
+
): Promise<EphemeralServiceContainer<T> & FluidContainerAttached<T>> {
|
|
501
|
+
EphemeralServiceImplementation.narrow(options.service);
|
|
502
|
+
const containerInner = await loadExistingContainer({
|
|
503
|
+
request: createLoadExistingRequest(id),
|
|
504
|
+
urlResolver,
|
|
505
|
+
documentServiceFactory: options.service.getDocumentServiceFactory(),
|
|
506
|
+
codeLoader: makeCodeLoader(
|
|
507
|
+
registry,
|
|
508
|
+
options.minVersionForCollaboration,
|
|
509
|
+
containerRuntimeLoader,
|
|
510
|
+
),
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
const container = new EphemeralServiceContainer<T>(
|
|
514
|
+
registry,
|
|
515
|
+
options,
|
|
516
|
+
containerInner,
|
|
517
|
+
(await containerInner.getEntryPoint()) as T,
|
|
518
|
+
id,
|
|
519
|
+
);
|
|
520
|
+
assert(
|
|
521
|
+
container.id !== undefined,
|
|
522
|
+
0xd14 /* id should be defined when loading a container */,
|
|
523
|
+
);
|
|
524
|
+
return container as typeof container & { id: string };
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
private constructor(
|
|
528
|
+
registry: Registry<Promise<DataStoreKind<TData>>>,
|
|
529
|
+
options: EphemeralServiceOptions,
|
|
530
|
+
container: IContainer,
|
|
531
|
+
data: TData,
|
|
532
|
+
id: string | undefined,
|
|
533
|
+
) {
|
|
534
|
+
super(registry, options, container, data, id);
|
|
535
|
+
this.service = options.service;
|
|
536
|
+
EphemeralServiceImplementation.narrow(this.service);
|
|
537
|
+
this.service.addContainer(this);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
public override close(): void {
|
|
541
|
+
super.close();
|
|
542
|
+
// Remove this now-closed container from its service's set of open containers.
|
|
543
|
+
EphemeralServiceImplementation.narrow(this.service);
|
|
544
|
+
this.service.removeContainer(this);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
protected createAttachRequest(): IRequest {
|
|
548
|
+
const documentId = (documentIdCounter++).toString();
|
|
549
|
+
return createLocalResolverCreateNewRequest(documentId);
|
|
550
|
+
}
|
|
551
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -11,3 +11,13 @@ export { LocalDocumentStorageService } from "./localDocumentStorageService.js";
|
|
|
11
11
|
export { createLocalResolverCreateNewRequest, LocalResolver } from "./localResolver.js";
|
|
12
12
|
export { localDriverCompatDetailsForLoader } from "./localLayerCompatState.js";
|
|
13
13
|
export { LocalSessionStorageDbFactory } from "./localSessionStorageDb.js";
|
|
14
|
+
export type {
|
|
15
|
+
EphemeralService,
|
|
16
|
+
EphemeralServiceClient,
|
|
17
|
+
EphemeralServiceOptions,
|
|
18
|
+
} from "./ephemeralService.js";
|
|
19
|
+
export {
|
|
20
|
+
startEphemeralService,
|
|
21
|
+
cleanupEphemeralService,
|
|
22
|
+
getDefaultEphemeralService,
|
|
23
|
+
} from "./ephemeralService.js";
|
package/src/packageVersion.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -5,5 +5,8 @@
|
|
|
5
5
|
"compilerOptions": {
|
|
6
6
|
"rootDir": "./src",
|
|
7
7
|
"outDir": "./lib",
|
|
8
|
+
// Disabled because this package imports container-runtime/container-loader, whose emitted
|
|
9
|
+
// declarations are incompatible with exactOptionalPropertyTypes.
|
|
10
|
+
"exactOptionalPropertyTypes": false,
|
|
8
11
|
},
|
|
9
12
|
}
|