-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathcompatibilityBase.ts
More file actions
392 lines (369 loc) · 18.2 KB
/
compatibilityBase.ts
File metadata and controls
392 lines (369 loc) · 18.2 KB
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { assert, fail } from "@fluidframework/core-utils/internal";
import type { MinimumVersionForCollab } from "@fluidframework/runtime-definitions/internal";
import { UsageError } from "@fluidframework/telemetry-utils/internal";
import { compare, gt, gte, lte, valid, parse } from "semver-ts";
import { pkgVersion } from "./packageVersion.js";
/**
* Our policy is to support major versions N and N-1, where N is most
* recent public major release of the Fluid Framework Client.
* Therefore, if the customer does not provide a minVersionForCollab, we will
* default to use N-1.
*
* However, this is not consistent with today's behavior. Some options (i.e.
* batching, compression) are enabled by default despite not being compatible
* with 1.x clients. Since the policy was introduced during 2.x's lifespan,
* N/N-1 compatibility by **default** will be in effect starting with 3.0.
* Importantly though, N/N-2 compatibility is still guaranteed with the proper
* configurations set.
*
* Further to distinguish unspecified `minVersionForCollab` from a specified
* version and allow `enableExplicitSchemaControl` to default to `true` for
* any 2.0.0+ version, we will use a special value of `2.0.0-defaults`, which
* is semantically less than 2.0.0.
*
* @internal
*/
export const defaultMinVersionForCollab =
"2.0.0-defaults" as const satisfies MinimumVersionForCollab;
/**
* We don't want allow a version before the major public release of the LTS version.
* Today we use "1.0.0", because our policy supports N/N-1 & N/N-2, which includes
* all minor versions of N. Though LTS starts at 1.4.0, we should stay consistent
* with our policy and allow all 1.x versions to be compatible with 2.x.
*
* @privateRemarks
* Exported for use in tests.
*
* @internal
*/
export const lowestMinVersionForCollab = "1.0.0" as const satisfies MinimumVersionForCollab;
/**
* String in a valid semver format specifying bottom of a minor version
* or special "defaults" prerelease of a major.
* @remarks Only 2.0.0-defaults is expected, but index signatures cannot be a
* literal; so, just allow any major -defaults prerelease.
*
* @internal
*/
export type MinimumMinorSemanticVersion = `${bigint}.${bigint}.0` | `${bigint}.0.0-defaults`;
/**
* String in a valid semver format of a specific version at least specifying minor.
* Unlike {@link @fluidframework/runtime-definitions#MinimumVersionForCollab}, this type allows any bigint for the major version.
* Used as a more generic type that allows major versions other than 1 or 2.
*
* @internal
*/
export type SemanticVersion =
| `${bigint}.${bigint}.${bigint}`
| `${bigint}.${bigint}.${bigint}-${string}`;
/**
* Converts a record into a configuration map that associates each key with an instance of its value type that is based on a {@link MinimumMinorSemanticVersion}.
* @remarks
* For a given input {@link @fluidframework/runtime-definitions#MinimumVersionForCollab},
* the corresponding configuration values can be found by using the entry in the inner objects with the highest {@link MinimumMinorSemanticVersion}
* that does not exceed the given {@link @fluidframework/runtime-definitions#MinimumVersionForCollab}.
*
* Use {@link getConfigsForMinVersionForCollab} to retrieve the configuration for a given a {@link @fluidframework/runtime-definitions#MinimumVersionForCollab}.
*
* See the remarks on {@link MinimumMinorSemanticVersion} for some limitation on how ConfigMaps must handle versioning.
* @internal
*/
export type ConfigMap<T extends Record<string, unknown>> = {
readonly [K in keyof T]-?: ConfigMapEntry<T[K]>;
};
/**
* Entry in {@link ConfigMap} associating {@link MinimumMinorSemanticVersion} with configuration values that became supported in that version.
* @remarks
* All entries must at least provide an entry for {@link lowestMinVersionForCollab}.
* @internal
*/
export interface ConfigMapEntry<T> {
// This index signature (See https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures) requires all properties on this type to to have keys that are a MinimumMinorSemanticVersion and values of type T.
// Note that the "version" part of this syntax is really just documentation and has no impact on the type checking (other than some identifier being required to the syntax here to differentiate it from the computed property syntax).
[version: MinimumMinorSemanticVersion]: T;
// Require an entry for the defaultMinVersionForCollab:
// this ensures that all versions of lowestMinVersionForCollab or later have a specified value in the ConfigMap.
// Note that this is NOT an index signature.
// This is a regular property with a computed name (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names).
[lowestMinVersionForCollab]: T;
}
/**
* Generic type for runtimeOptionsAffectingDocSchemaConfigValidationMap
*
* @internal
*/
export type ConfigValidationMap<T extends Record<string, unknown>> = {
readonly [K in keyof T]-?: (configValue: T[K]) => SemanticVersion | undefined;
};
/**
* Returns a default configuration given minVersionForCollab and configuration version map.
*
* @privateRemarks
* The extra `Record` type for the `configMap` is just used to allow the body of this function to be more type-safe due to limitations of generic types in TypeScript.
* It should have no impact on the user of this function.
* @internal
*/
export function getConfigsForMinVersionForCollab<T extends Record<SemanticVersion, unknown>>(
minVersionForCollab: MinimumVersionForCollab,
configMap: ConfigMap<T> & Record<keyof T, unknown>,
): T {
validateMinimumVersionForCollab(minVersionForCollab);
const defaultConfigs: Partial<T> = {};
// Iterate over configMap to get default values for each option.
for (const [key, config] of Object.entries(configMap)) {
defaultConfigs[key] = getConfigForMinVersionForCollab(
minVersionForCollab,
config as ConfigMapEntry<unknown>,
);
}
// We have populated every key, so casting away the Partial is now safe:
return defaultConfigs as T;
}
/**
* Returns a default configuration given minVersionForCollab and {@link ConfigMapEntry}.
*
* @internal
*/
export function getConfigForMinVersionForCollab<T>(
minVersionForCollab: MinimumVersionForCollab,
config: ConfigMapEntry<T>,
): T {
return getConfigForMinVersionForCollabIterable(
minVersionForCollab,
Object.entries(config) as [MinimumMinorSemanticVersion, T][],
);
}
/**
* Returns a default configuration given minVersionForCollab and the contents of a {@link ConfigMapEntry} in an Iterable.
* @remarks
* See also {@link getConfigForMinVersionForCollab} for consuming a ConfigMapEntry directly.
*
* `ConfigMapEntry` is a nice type safe format for developers to directly author this data,
* but it is messy and less type safe to work with it in this format programmatically.
* Thus this function exists to help meet the needs of programmatic use cases,
* like cases which transform a ConfigMapEntry before selecting a value from it.
* @internal
*/
export function getConfigForMinVersionForCollabIterable<T>(
minVersionForCollab: MinimumVersionForCollab,
entries: Iterable<readonly [MinimumMinorSemanticVersion | MinimumVersionForCollab, T]>, // [[typeof lowestMinVersionForCollab, T], ...[MinimumVersionForCollab, T][]],
): T {
// Validate and strongly type the versions from the configMap.
const versions: [MinimumVersionForCollab, T][] = Array.from(entries, ([version, value]) => {
validateMinimumVersionForCollab(version);
return [version, value];
});
return (selectVersionRoundedDown(minVersionForCollab, versions) ??
fail(0xcb8 /* No config map entry for version */))[1];
}
/**
* Finds the entry for the highest version that is less than or equal to the provided minVersionForCollab.
* @remarks
* If none is found, returns undefined.
*
* When used with Fluid client versions, use the stricter {@link getConfigForMinVersionForCollabIterable} instead.
*
* @internal
*/
export function selectVersionRoundedDown<T>(
minVersionForCollab: string,
entries: Iterable<readonly [string, T]>,
compareVersions: (a: string, b: string) => number = compare,
): readonly [string, T] | undefined {
// Sort a copy of the iterable in descending order
const versions: (readonly [string, T])[] = [...entries];
versions.sort((a, b) => compareVersions(b[0], a[0]));
// For each config, we iterate over the keys and check if minVersionForCollab is greater than or equal to the version.
// If so, we set it as the default value for the option.
for (const pair of versions) {
const [version, _] = pair;
if (compareVersions(minVersionForCollab, version) >= 0) {
return pair;
}
}
return undefined;
}
/**
* Returns detailed information about the validity of a minVersionForCollab.
* @param minVersionForCollab - The minVersionForCollab to validate.
* @returns An object containing the validity information.
*
* @internal
*/
export function checkValidMinVersionForCollabVerbose(minVersionForCollab: SemanticVersion): {
isValidSemver: boolean;
isGteLowestMinVersion: boolean;
isLtePkgVersion: boolean;
} {
const isValidSemver = valid(minVersionForCollab) !== null;
return {
isValidSemver,
// We have to check if the value is a valid semver before calling gte/lte, otherwise they will throw when parsing the version.
isGteLowestMinVersion:
isValidSemver && gte(minVersionForCollab, lowestMinVersionForCollab),
isLtePkgVersion: isValidSemver && lte(minVersionForCollab, cleanedPackageVersion),
};
}
/**
* Checks if the minVersionForCollab is valid.
* A valid minVersionForCollab is a MinimumVersionForCollab that is at least `lowestMinVersionForCollab` and less than or equal to the current package version.
*
* @internal
*/
export function isValidMinVersionForCollab(
minVersionForCollab: SemanticVersion,
): minVersionForCollab is MinimumVersionForCollab {
const { isValidSemver, isGteLowestMinVersion, isLtePkgVersion } =
checkValidMinVersionForCollabVerbose(minVersionForCollab);
return isValidSemver && isGteLowestMinVersion && isLtePkgVersion;
}
const parsedPackageVersion = parse(pkgVersion) ?? fail(0xcb9 /* Invalid package version */);
/**
* `pkgVersion` version without pre-release.
* @remarks
* This is the version that the code in the current version of the codebase will have when officially released.
* Generally, compatibility of prerelease builds is not guaranteed (especially for how they interact with future releases).
* So while technically a prerelease build is less (older) than the released version which follows it and thus supports less features,
* it makes sense for them to claim to support the same features as the following release so they can be used to test how the release would actually behave.
*
* To accomplish this, the version the next release will have is provided here as `cleanedPackageVersion` while `pkgVersion` may be a prerelease in some cases,
* like when running tests on CI, or in an actual prerelease published package.
* This is then used in {@link validateMinimumVersionForCollab} to allow the version shown on main to be usable as a `minVersionForCollab`, even in CI and prerelease packages.
*
* This is of particular note in two cases:
* 1. When landing a new feature, and setting the minVersionForCollab which enables it to be the version that the next release will have.
* Having that version be valid on main, pass tests locally, then fail on CI and when using published prerelease packages would be confusing, and probably undesired.
* 2. Setting the minVersionForCollab to the current version for scenarios that do no involve collab with other package versions seems like it should be valid.
* This is useful for testing new features, and also non collaborative scenarios where the latest features are desired.
*
* To accommodate some uses of the second case, it might be useful to package export this in the future.
*
* @privateRemarks
* Since this is used by validateMinimumVersionForCollab, the type case to MinimumVersionForCollab can not use it directly.
* Thus this is just `as` cast here, and a test confirms it is valid according to validateMinimumVersionForCollab.
*
* @internal
*/
export const cleanedPackageVersion =
`${parsedPackageVersion.major}.${parsedPackageVersion.minor}.${parsedPackageVersion.patch}` as MinimumVersionForCollab;
/**
* Narrows the type of the provided {@link SemanticVersion} to a {@link @fluidframework/runtime-definitions#MinimumVersionForCollab}, throwing a UsageError if it is not valid.
* @remarks
* This is more strict than the type constraints imposed by `MinimumVersionForCollab`.
* Currently there is no type which is used to separate semantically valid and typescript allowed MinimumVersionForCollab values:
* thus users that care about strict validation may want to call this on un-validated `MinimumVersionForCollab` values.
* @param semanticVersion - The version to check.
* @throws UsageError if the version is not a valid MinimumVersionForCollab.
*
* @internal
*/
export function validateMinimumVersionForCollab(
semanticVersion: string,
): asserts semanticVersion is MinimumVersionForCollab {
const minVersionForCollab = semanticVersion as MinimumVersionForCollab;
const { isValidSemver, isGteLowestMinVersion, isLtePkgVersion } =
checkValidMinVersionForCollabVerbose(minVersionForCollab);
if (!(isValidSemver && isGteLowestMinVersion && isLtePkgVersion)) {
throw new UsageError(
`Version ${minVersionForCollab} is not a valid MinimumVersionForCollab. ` +
`It must be in a valid semver format, at least ${lowestMinVersionForCollab}, ` +
`and less than or equal to the current package version ${cleanedPackageVersion}. ` +
`Details: { isValidSemver: ${isValidSemver}, isGteLowestMinVersion: ${isGteLowestMinVersion}, isLtePkgVersion: ${isLtePkgVersion} }`,
);
}
}
/**
* Validates the given `overrides`.
*
* No-op when minVersionForCollab is set to defaultMinVersionForCollab.
*
* Otherwise this checks that for keys which are in both the `validationMap` and the `overrides`,
* that the `validationMap` function for that key either returns undefined or a version less than or equal to `minVersionForCollab`.
* @privateRemarks
* This design seems odd, and might want to be revisited.
* Currently it only permits opting out of features, not into them (unless validationMap returns undefined),
* and the handling of defaultMinVersionForCollab and undefined versions seems questionable.
* Also ignoring of extra keys in overrides might be bad since it seems like overrides is supposed to be validated.
* @internal
*/
export function validateConfigMapOverrides<T extends Record<string, unknown>>(
minVersionForCollab: SemanticVersion,
overrides: Partial<T>,
validationMap: ConfigValidationMap<T>,
): void {
if (minVersionForCollab === defaultMinVersionForCollab) {
// If the minVersionForCollab is set to the default value, then we will not validate the runtime options
// This is to avoid disruption to users who have not yet set the minVersionForCollab value explicitly.
// TODO: This also skips validation for users which explicitly request defaultMinVersionForCollab which seems like a bug.
return;
}
// Iterate through each runtime option passed in by the user
// Type assertion is safe as entries come from runtimeOptions object
for (const [passedRuntimeOption, passedRuntimeOptionValue] of Object.entries(overrides) as [
keyof T & string,
T[keyof T & string],
][]) {
// Skip if passedRuntimeOption is not in validation map
if (!(passedRuntimeOption in validationMap)) {
continue;
}
const requiredVersion = validationMap[passedRuntimeOption](passedRuntimeOptionValue);
if (requiredVersion !== undefined && gt(requiredVersion, minVersionForCollab)) {
throw new UsageError(
`Runtime option ${passedRuntimeOption}:${JSON.stringify(passedRuntimeOptionValue)} requires ` +
`runtime version ${requiredVersion}. Please update minVersionForCollab ` +
`(currently ${minVersionForCollab}) to ${requiredVersion} or later to proceed.`,
);
}
}
}
/**
* Helper function to map ContainerRuntimeOptionsInternal config values to
* minVersionForCollab in, e.g., {@link @fluidframework/container-runtime#runtimeOptionsAffectingDocSchemaConfigValidationMap}.
*
* @internal
*/
export function configValueToMinVersionForCollab<
T extends string | number | boolean | undefined | object,
Arr extends readonly [T, SemanticVersion][],
>(configToMinVer: Arr): (configValue: T) => SemanticVersion | undefined {
const configValueToRequiredVersionMap = new Map(configToMinVer);
return (configValue: T) => {
// If the configValue is not an object then we can get the version required directly from the map.
if (typeof configValue !== "object") {
return configValueToRequiredVersionMap.get(configValue);
}
// When the input `configValue` is an object, this logic determines the minimum runtime version it requires.
// It iterates through each entry in `configValueToRequiredVersionMap`. If `possibleConfigValue` shares at
// least one key-value pair with the input `configValue`, its associated `versionRequired` is collected into
// `matchingVersions`. After checking all entries, the highest among the collected versions is returned.
// This represents the overall minimum version required to support the features implied by the input `configValue`.
const matchingVersions: SemanticVersion[] = [];
for (const [
possibleConfigValue,
versionRequired,
] of configValueToRequiredVersionMap.entries()) {
assert(
typeof possibleConfigValue == "object",
0xbb9 /* possibleConfigValue should be an object */,
);
// Check if `possibleConfigValue` and the input `configValue` share at least one
// common key-value pair. If they do, the `versionRequired` for this `possibleConfigValue`
// is added to `matchingVersions`.
if (Object.entries(possibleConfigValue).some(([k, v]) => configValue[k] === v)) {
matchingVersions.push(versionRequired);
}
}
if (matchingVersions.length > 0) {
// Return the latest minVersionForCollab among all matches.
return matchingVersions.sort((a, b) => compare(b, a))[0];
}
// If no matches then we return undefined. This means that the config value passed in
// does not require a specific minVersionForCollab to be valid.
return undefined;
};
}