-
Notifications
You must be signed in to change notification settings - Fork 535
/
loader.ts
503 lines (445 loc) · 17.2 KB
/
loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { v4 as uuid } from "uuid";
import { ITelemetryBaseLogger, ITelemetryLogger } from "@fluidframework/common-definitions";
import {
FluidObject,
IFluidCodeDetails,
IFluidRouter,
IProvideFluidCodeDetailsComparer,
IRequest,
IRequestHeader,
IResponse,
} from "@fluidframework/core-interfaces";
import {
ICodeLoader,
IContainer,
IFluidModule,
IHostLoader,
ILoader,
IPendingLocalState,
ILoaderOptions as ILoaderOptions1,
IProxyLoaderFactory,
LoaderHeader,
} from "@fluidframework/container-definitions";
import {
ChildLogger,
DebugLogger,
IConfigProviderBase,
loggerToMonitoringContext,
mixinMonitoringContext,
MonitoringContext,
PerformanceEvent,
sessionStorageConfigProvider,
} from "@fluidframework/telemetry-utils";
import {
IDocumentServiceFactory,
IDocumentStorageService,
IFluidResolvedUrl,
IResolvedUrl,
IUrlResolver,
} from "@fluidframework/driver-definitions";
import { ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
import {
ensureFluidResolvedUrl,
MultiUrlResolver,
MultiDocumentServiceFactory,
} from "@fluidframework/driver-utils";
import { Container } from "./container";
import { IParsedUrl, parseUrl } from "./utils";
import { pkgVersion } from "./packageVersion";
function canUseCache(request: IRequest): boolean {
if (request.headers === undefined) {
return true;
}
return request.headers[LoaderHeader.cache] !== false;
}
export class RelativeLoader implements ILoader {
constructor(
private readonly container: Container,
private readonly loader: ILoader | undefined,
) {
}
public get IFluidRouter(): IFluidRouter { return this; }
public async resolve(request: IRequest): Promise<IContainer> {
if (request.url.startsWith("/")) {
if (canUseCache(request)) {
return this.container;
} else {
const resolvedUrl = this.container.resolvedUrl;
ensureFluidResolvedUrl(resolvedUrl);
const container = await Container.load(
this.loader as Loader,
{
canReconnect: request.headers?.[LoaderHeader.reconnect],
clientDetailsOverride: request.headers?.[LoaderHeader.clientDetails],
resolvedUrl: {...resolvedUrl},
version: request.headers?.[LoaderHeader.version] ?? undefined,
loadMode: request.headers?.[LoaderHeader.loadMode],
},
);
return container;
}
}
if (this.loader === undefined) {
throw new Error("Cannot resolve external containers");
}
return this.loader.resolve(request);
}
public async request(request: IRequest): Promise<IResponse> {
if (request.url.startsWith("/")) {
const container = await this.resolve(request);
return container.request(request);
}
if (this.loader === undefined) {
return {
status: 404,
value: "Cannot request external containers",
mimeType: "plain/text",
};
}
return this.loader.request(request);
}
}
function createCachedResolver(resolver: IUrlResolver) {
const cacheResolver = Object.create(resolver) as IUrlResolver;
const resolveCache = new Map<string, Promise<IResolvedUrl | undefined>>();
cacheResolver.resolve = async (request: IRequest): Promise<IResolvedUrl | undefined> => {
if (!canUseCache(request)) {
return resolver.resolve(request);
}
if (!resolveCache.has(request.url)) {
resolveCache.set(request.url, resolver.resolve(request));
}
return resolveCache.get(request.url);
};
return cacheResolver;
}
export interface ILoaderOptions extends ILoaderOptions1{
summarizeProtocolTree?: boolean,
}
/**
* @deprecated IFluidModuleWithDetails interface is moved to
* {@link @fluidframework/container-definition#IFluidModuleWithDetails}
* to have all the code loading modules in one package. #8193
* Encapsulates a module entry point with corresponding code details.
*/
export interface IFluidModuleWithDetails {
/** Fluid code module that implements the runtime factory needed to instantiate the container runtime. */
module: IFluidModule;
/**
* Code details associated with the module. Represents a document schema this module supports.
* If the code loader implements the {@link @fluidframework/core-interfaces#IFluidCodeDetailsComparer} interface,
* it'll be called to determine whether the module code details satisfy the new code proposal in the quorum.
*/
details: IFluidCodeDetails;
}
/**
* @deprecated ICodeDetailsLoader interface is moved to {@link @fluidframework/container-definition#ICodeDetailsLoader}
* to have code loading modules in one package. #8193
* Fluid code loader resolves a code module matching the document schema, i.e. code details, such as
* a package name and package version range.
*/
export interface ICodeDetailsLoader
extends Partial<IProvideFluidCodeDetailsComparer> {
/**
* Load the code module (package) that is capable to interact with the document.
*
* @param source - Code proposal that articulates the current schema the document is written in.
* @returns - Code module entry point along with the code details associated with it.
*/
load(source: IFluidCodeDetails): Promise<IFluidModuleWithDetails>;
}
/**
* Services and properties necessary for creating a loader
*/
export interface ILoaderProps {
/**
* The url resolver used by the loader for resolving external urls
* into Fluid urls such that the container specified by the
* external url can be loaded.
*/
readonly urlResolver: IUrlResolver;
/**
* The document service factory take the Fluid url provided
* by the resolved url and constructs all the necessary services
* for communication with the container's server.
*/
readonly documentServiceFactory: IDocumentServiceFactory;
/**
* The code loader handles loading the necessary code
* for running a container once it is loaded.
*/
readonly codeLoader: ICodeDetailsLoader | ICodeLoader;
/**
* A property bag of options used by various layers
* to control features
*/
readonly options?: ILoaderOptions;
/**
* Scope is provided to all container and is a set of shared
* services for container's to integrate with their host environment.
*/
readonly scope?: FluidObject;
/**
* Proxy loader factories for loading containers via proxy in other contexts,
* like web workers, or worker threads.
*/
readonly proxyLoaderFactories?: Map<string, IProxyLoaderFactory>;
/**
* The logger that all telemetry should be pushed to.
*/
readonly logger?: ITelemetryBaseLogger;
/**
* Blobs storage for detached containers.
*/
readonly detachedBlobStorage?: IDetachedBlobStorage;
/**
* The configuration provider which may be used to control features.
*/
readonly configProvider?: IConfigProviderBase;
}
/**
* Services and properties used by and exposed by the loader
*/
export interface ILoaderServices {
/**
* The url resolver used by the loader for resolving external urls
* into Fluid urls such that the container specified by the
* external url can be loaded.
*/
readonly urlResolver: IUrlResolver;
/**
* The document service factory take the Fluid url provided
* by the resolved url and constructs all the necessary services
* for communication with the container's server.
*/
readonly documentServiceFactory: IDocumentServiceFactory;
/**
* The code loader handles loading the necessary code
* for running a container once it is loaded.
*/
readonly codeLoader: ICodeDetailsLoader | ICodeLoader;
/**
* A property bag of options used by various layers
* to control features
*/
readonly options: ILoaderOptions;
/**
* Scope is provided to all container and is a set of shared
* services for container's to integrate with their host environment.
*/
readonly scope: FluidObject;
/**
* Proxy loader factories for loading containers via proxy in other contexts,
* like web workers, or worker threads.
*/
readonly proxyLoaderFactories: Map<string, IProxyLoaderFactory>;
/**
* The logger downstream consumers should construct their loggers from
*/
readonly subLogger: ITelemetryLogger;
/**
* Blobs storage for detached containers.
*/
readonly detachedBlobStorage?: IDetachedBlobStorage;
}
/**
* Subset of IDocumentStorageService which only supports createBlob() and readBlob(). This is used to support
* blobs in detached containers.
*/
export type IDetachedBlobStorage = Pick<IDocumentStorageService, "createBlob" | "readBlob"> & {
size: number;
/**
* Return an array of all blob IDs present in storage
*/
getBlobIds(): string[];
};
/**
* Manages Fluid resource loading
*/
export class Loader implements IHostLoader {
private readonly containers = new Map<string, Promise<Container>>();
public readonly services: ILoaderServices;
private readonly mc: MonitoringContext;
constructor(loaderProps: ILoaderProps) {
const scope = { ...loaderProps.scope as FluidObject<ILoader> };
if (loaderProps.options?.provideScopeLoader !== false) {
scope.ILoader = this;
}
const telemetryProps = {
loaderId: uuid(),
loaderVersion: pkgVersion,
};
const subMc = mixinMonitoringContext(
DebugLogger.mixinDebugLogger("fluid:telemetry", loaderProps.logger, { all: telemetryProps }),
sessionStorageConfigProvider.value,
loaderProps.configProvider,
);
this.services = {
urlResolver: createCachedResolver(MultiUrlResolver.create(loaderProps.urlResolver)),
documentServiceFactory: MultiDocumentServiceFactory.create(loaderProps.documentServiceFactory),
codeLoader: loaderProps.codeLoader,
options: loaderProps.options ?? {},
scope,
subLogger: subMc.logger,
proxyLoaderFactories: loaderProps.proxyLoaderFactories ?? new Map<string, IProxyLoaderFactory>(),
detachedBlobStorage: loaderProps.detachedBlobStorage,
};
this.mc = loggerToMonitoringContext(
ChildLogger.create(this.services.subLogger, "Loader"));
}
public get IFluidRouter(): IFluidRouter { return this; }
public async createDetachedContainer(codeDetails: IFluidCodeDetails): Promise<IContainer> {
const container = await Container.createDetached(
this,
codeDetails,
);
if (this.cachingEnabled) {
container.once("attached", () => {
ensureFluidResolvedUrl(container.resolvedUrl);
const parsedUrl = parseUrl(container.resolvedUrl.url);
if (parsedUrl !== undefined) {
this.addToContainerCache(parsedUrl.id, Promise.resolve(container));
}
});
}
return container;
}
public async rehydrateDetachedContainerFromSnapshot(snapshot: string): Promise<IContainer> {
return Container.rehydrateDetachedFromSnapshot(this, snapshot);
}
public async resolve(request: IRequest, pendingLocalState?: string): Promise<IContainer> {
const eventName = pendingLocalState === undefined ? "Resolve" : "ResolveWithPendingState";
return PerformanceEvent.timedExecAsync(this.mc.logger, { eventName }, async () => {
const resolved = await this.resolveCore(
request,
pendingLocalState !== undefined ? JSON.parse(pendingLocalState) : undefined,
);
return resolved.container;
});
}
public async request(request: IRequest): Promise<IResponse> {
return PerformanceEvent.timedExecAsync(this.mc.logger, { eventName: "Request" }, async () => {
const resolved = await this.resolveCore(request);
return resolved.container.request({
...request,
url: `${resolved.parsed.path}${resolved.parsed.query}`,
});
});
}
private getKeyForContainerCache(request: IRequest, parsedUrl: IParsedUrl): string {
const key = request.headers?.[LoaderHeader.version] !== undefined
? `${parsedUrl.id}@${request.headers[LoaderHeader.version]}`
: parsedUrl.id;
return key;
}
private addToContainerCache(key: string, containerP: Promise<Container>) {
this.containers.set(key, containerP);
containerP.then((container) => {
// If the container is closed or becomes closed after we resolve it, remove it from the cache.
if (container.closed) {
this.containers.delete(key);
} else {
container.once("closed", () => {
this.containers.delete(key);
});
}
}).catch((error) => {});
}
private async resolveCore(
request: IRequest,
pendingLocalState?: IPendingLocalState,
): Promise<{ container: Container; parsed: IParsedUrl }> {
const resolvedAsFluid = await this.services.urlResolver.resolve(request);
ensureFluidResolvedUrl(resolvedAsFluid);
// Parse URL into data stores
const parsed = parseUrl(resolvedAsFluid.url);
if (parsed === undefined) {
throw new Error(`Invalid URL ${resolvedAsFluid.url}`);
}
if (pendingLocalState !== undefined) {
const parsedPendingUrl = parseUrl(pendingLocalState.url);
if (parsedPendingUrl?.id !== parsed.id ||
parsedPendingUrl?.path.replace(/\/$/, "") !== parsed.path.replace(/\/$/, "")) {
const message = `URL ${resolvedAsFluid.url} does not match pending state URL ${pendingLocalState.url}`;
throw new Error(message);
}
}
const { canCache, fromSequenceNumber } = this.parseHeader(parsed, request);
const shouldCache = pendingLocalState !== undefined ? false : canCache;
let container: Container;
if (shouldCache) {
const key = this.getKeyForContainerCache(request, parsed);
const maybeContainer = await this.containers.get(key);
if (maybeContainer !== undefined) {
container = maybeContainer;
} else {
const containerP =
this.loadContainer(
request,
resolvedAsFluid);
this.addToContainerCache(key, containerP);
container = await containerP;
}
} else {
container =
await this.loadContainer(
request,
resolvedAsFluid,
pendingLocalState?.pendingRuntimeState);
}
if (container.deltaManager.lastSequenceNumber <= fromSequenceNumber) {
await new Promise<void>((resolve, reject) => {
function opHandler(message: ISequencedDocumentMessage) {
if (message.sequenceNumber > fromSequenceNumber) {
resolve();
container.removeListener("op", opHandler);
}
}
container.on("op", opHandler);
});
}
return { container, parsed };
}
private get cachingEnabled() {
return this.services.options.cache !== false;
}
private canCacheForRequest(headers: IRequestHeader): boolean {
return this.cachingEnabled && headers[LoaderHeader.cache] !== false;
}
private parseHeader(parsed: IParsedUrl, request: IRequest) {
let fromSequenceNumber = -1;
request.headers = request.headers ?? {};
const headerSeqNum = request.headers[LoaderHeader.sequenceNumber];
if (headerSeqNum !== undefined) {
fromSequenceNumber = headerSeqNum;
}
// If set in both query string and headers, use query string
request.headers[LoaderHeader.version] = parsed.version ?? request.headers[LoaderHeader.version];
const canCache = this.canCacheForRequest(request.headers);
return {
canCache,
fromSequenceNumber,
};
}
private async loadContainer(
request: IRequest,
resolved: IFluidResolvedUrl,
pendingLocalState?: unknown,
): Promise<Container> {
return Container.load(
this,
{
canReconnect: request.headers?.[LoaderHeader.reconnect],
clientDetailsOverride: request.headers?.[LoaderHeader.clientDetails],
resolvedUrl: resolved,
version: request.headers?.[LoaderHeader.version] ?? undefined,
loadMode: request.headers?.[LoaderHeader.loadMode],
},
pendingLocalState,
);
}
}