@fluidframework/id-compressor 3.2.0 → 3.4.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 +15 -0
- package/api-report/id-compressor.legacy.beta.api.md +0 -49
- package/dist/idCompressor.d.ts +65 -22
- package/dist/idCompressor.d.ts.map +1 -1
- package/dist/idCompressor.js +401 -60
- package/dist/idCompressor.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/legacy.d.ts +1 -12
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/test/idCompressor.perf.spec.js.map +1 -1
- package/dist/test/idCompressor.spec.js.map +1 -1
- package/dist/test/idCompressorTestUtilities.d.ts.map +1 -1
- package/dist/test/idCompressorTestUtilities.js +8 -3
- package/dist/test/idCompressorTestUtilities.js.map +1 -1
- package/dist/test/sharding.fuzz.spec.d.ts +6 -0
- package/dist/test/sharding.fuzz.spec.d.ts.map +1 -0
- package/dist/test/sharding.fuzz.spec.js.map +1 -0
- package/dist/test/sharding.spec.d.ts +6 -0
- package/dist/test/sharding.spec.d.ts.map +1 -0
- package/dist/test/sharding.spec.js.map +1 -0
- package/dist/test/snapshots/summary.spec.js.map +1 -1
- package/dist/test/types/validateIdCompressorPrevious.generated.js.map +1 -1
- package/dist/types/idCompressor.d.ts +205 -20
- package/dist/types/idCompressor.d.ts.map +1 -1
- package/dist/types/idCompressor.js +15 -0
- package/dist/types/idCompressor.js.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/persisted-types/0.0.1.d.ts +4 -4
- package/dist/types/persisted-types/0.0.1.js.map +1 -1
- package/dist/utilities.d.ts +1 -1
- package/dist/utilities.js +1 -1
- package/dist/utilities.js.map +1 -1
- package/lib/idCompressor.d.ts +65 -22
- package/lib/idCompressor.d.ts.map +1 -1
- package/lib/idCompressor.js +402 -61
- package/lib/idCompressor.js.map +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/legacy.d.ts +1 -12
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/test/idCompressor.perf.spec.js.map +1 -1
- package/lib/test/idCompressor.spec.js.map +1 -1
- package/lib/test/idCompressorTestUtilities.d.ts.map +1 -1
- package/lib/test/idCompressorTestUtilities.js +8 -3
- package/lib/test/idCompressorTestUtilities.js.map +1 -1
- package/lib/test/sharding.fuzz.spec.d.ts +6 -0
- package/lib/test/sharding.fuzz.spec.d.ts.map +1 -0
- package/lib/test/sharding.fuzz.spec.js.map +1 -0
- package/lib/test/sharding.spec.d.ts +6 -0
- package/lib/test/sharding.spec.d.ts.map +1 -0
- package/lib/test/sharding.spec.js.map +1 -0
- package/lib/test/snapshots/summary.spec.js.map +1 -1
- package/lib/test/types/validateIdCompressorPrevious.generated.js.map +1 -1
- package/lib/types/idCompressor.d.ts +205 -20
- package/lib/types/idCompressor.d.ts.map +1 -1
- package/lib/types/idCompressor.js +14 -1
- package/lib/types/idCompressor.js.map +1 -1
- package/lib/types/index.d.ts +2 -1
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/persisted-types/0.0.1.d.ts +4 -4
- package/lib/types/persisted-types/0.0.1.js.map +1 -1
- package/lib/utilities.d.ts +1 -1
- package/lib/utilities.js +1 -1
- package/lib/utilities.js.map +1 -1
- package/package.json +8 -8
- package/src/idCompressor.ts +550 -78
- package/src/index.ts +11 -5
- package/src/packageVersion.ts +1 -1
- package/src/test/idCompressorTestUtilities.ts +16 -3
- package/src/test/snapshots/v2/compressor-with-finalized-range-from-one-client +1 -0
- package/src/test/snapshots/v2/compressor-with-finalized-range-from-one-client-client2 +1 -0
- package/src/test/snapshots/v2/compressors-with-finalized-ranges-from-two-clients +1 -0
- package/src/test/snapshots/v2/compressors-with-finalized-ranges-from-two-clients-client2 +1 -0
- package/src/test/snapshots/v2/empty-compressor-summary +1 -0
- package/src/test/snapshots/v2/expansion-semantics +1 -0
- package/src/test/snapshots/v2/expansion-semantics-client2 +1 -0
- package/src/test/snapshots/v3/compressor-with-finalized-range-from-one-client +1 -0
- package/src/test/snapshots/v3/compressor-with-finalized-range-from-one-client-client2 +1 -0
- package/src/test/snapshots/v3/compressors-with-finalized-ranges-from-two-clients +1 -0
- package/src/test/snapshots/v3/compressors-with-finalized-ranges-from-two-clients-client2 +1 -0
- package/src/test/snapshots/v3/empty-compressor-summary +1 -0
- package/src/test/snapshots/v3/expansion-semantics +1 -0
- package/src/test/snapshots/v3/expansion-semantics-client2 +1 -0
- package/src/test/types/validateIdCompressorPrevious.generated.ts +1 -109
- package/src/types/idCompressor.ts +225 -20
- package/src/types/index.ts +13 -1
- package/src/types/persisted-types/0.0.1.ts +4 -4
- package/src/utilities.ts +1 -1
|
@@ -3,7 +3,27 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import type { OpSpaceCompressedId, SessionId, SessionSpaceCompressedId, StableId } from "./identifiers.js";
|
|
6
|
+
import type { Listenable } from "@fluidframework/core-interfaces";
|
|
6
7
|
import type { IdCreationRange, SerializedIdCompressorWithNoSession, SerializedIdCompressorWithOngoingSession } from "./persisted-types/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Serialization format versions for IdCompressor.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare const SerializationVersion: {
|
|
13
|
+
/**
|
|
14
|
+
* Base format without sharding support
|
|
15
|
+
*/
|
|
16
|
+
readonly V2: 2;
|
|
17
|
+
/**
|
|
18
|
+
* Adds optional sharding state
|
|
19
|
+
*/
|
|
20
|
+
readonly V3: 3;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Type representing valid serialization version values.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export type SerializationVersion = (typeof SerializationVersion)[keyof typeof SerializationVersion];
|
|
7
27
|
/**
|
|
8
28
|
* A distributed UUID generator and compressor.
|
|
9
29
|
*
|
|
@@ -68,12 +88,19 @@ import type { IdCreationRange, SerializedIdCompressorWithNoSession, SerializedId
|
|
|
68
88
|
* @internal
|
|
69
89
|
*/
|
|
70
90
|
export interface IIdCompressorCore {
|
|
91
|
+
/**
|
|
92
|
+
* Events emitted by the ID compressor.
|
|
93
|
+
*/
|
|
94
|
+
readonly events: Listenable<IdCompressorEvents>;
|
|
71
95
|
/**
|
|
72
96
|
* Returns a range of IDs created by this session in a format for sending to the server for finalizing.
|
|
73
97
|
* The range will include all IDs generated via calls to `generateCompressedId` since the last time a
|
|
74
98
|
* range was taken (via this method or `takeUnfinalizedCreationRange`).
|
|
75
99
|
* @returns the range of IDs, which may be empty. This range must be sent to the server for ordering before
|
|
76
100
|
* it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.
|
|
101
|
+
* @throws if called on a child (non-root) shard. All shards in a shard tree share a single session, and only
|
|
102
|
+
* the root shard may finalize that session's IDs with the server; a child's IDs are instead reconciled with the
|
|
103
|
+
* root via {@link IIdCompressorCore.synchronizeWithShard} / {@link IIdCompressorCore.disposeShard}.
|
|
77
104
|
*/
|
|
78
105
|
takeNextCreationRange(): IdCreationRange;
|
|
79
106
|
/**
|
|
@@ -127,37 +154,186 @@ export interface IIdCompressorCore {
|
|
|
127
154
|
*/
|
|
128
155
|
beginGhostSession(ghostSessionId: SessionId, ghostSessionCallback: () => void): void;
|
|
129
156
|
/**
|
|
130
|
-
*
|
|
157
|
+
* Shards the ID space of this compressor such that multiple local compressors can safely share it without colliding.
|
|
158
|
+
* This can allow multiple local instantiations of the same compressor to safely share an ID space in scenarios where
|
|
159
|
+
* different threads do not have access to a central ID compressor.
|
|
160
|
+
* @param newShardCount - The number of additional different shards to split this compressor into.
|
|
161
|
+
* Must be a positive safe integer.
|
|
162
|
+
* @throws If `newShardCount` is not a positive safe integer or the resulting stride exceeds the sharding limit.
|
|
163
|
+
* @returns An array of `newShardCount` {@link SerializedIdCompressorShard} objects.
|
|
164
|
+
* Note that this method should only be needed when multiple JS runtimes are in play, as sharded compressors essentially
|
|
165
|
+
* attempt to emulate a single static compressor and any code running in the same JS runtime can simply use statics.
|
|
166
|
+
*/
|
|
167
|
+
shard(newShardCount: number): SerializedIdCompressorShard[];
|
|
168
|
+
/**
|
|
169
|
+
* Synchronizes `this` compressor with a child shard. Synchronization will occur for the state of the child at the time `syncToken`
|
|
170
|
+
* was generated, meaning that `this` compressor can use/ingest IDs generated up to that point. Attempts to use IDs from a child shard
|
|
171
|
+
* without first synchronizing will result in an exception.
|
|
172
|
+
*
|
|
173
|
+
* If `syncToken` is a disposal token (its {@link ShardToken.disposed} flag is `true`, as produced by
|
|
174
|
+
* {@link IIdCompressorCore.disposeShard}), this additionally deregisters the child shard and reclaims its subset of the ID space
|
|
175
|
+
* into `this`. Once a shard has been disposed it is no longer safe to use the compressor the token came from, and a shard tree must
|
|
176
|
+
* be disposed from the leaves upwards.
|
|
177
|
+
* @param syncToken - The token for the shard, obtained by calling {@link IIdCompressorCore.getShardSyncToken} (non-destructive
|
|
178
|
+
* synchronization) or {@link IIdCompressorCore.disposeShard} (synchronization plus reclamation of the disposed shard's ID space).
|
|
179
|
+
* @throws `TypeError` if the token's generation count is not a nonnegative safe integer.
|
|
180
|
+
*/
|
|
181
|
+
synchronizeWithShard(syncToken: ShardSynchronizationToken): void;
|
|
182
|
+
/**
|
|
183
|
+
* Gets a synchronization token from this compressor for an active child shard.
|
|
184
|
+
*
|
|
185
|
+
* @remarks
|
|
186
|
+
* The child can apply this token without replacing its compressor.
|
|
187
|
+
*
|
|
188
|
+
* @param childToken - A token from the child shard that will receive this token.
|
|
189
|
+
* @returns This parent's synchronization token for that child.
|
|
190
|
+
* @throws If the child shard is not active on this compressor.
|
|
191
|
+
*/
|
|
192
|
+
getChildShardSyncToken(childToken: ShardSynchronizationToken): ParentShardSynchronizationToken;
|
|
193
|
+
/**
|
|
194
|
+
* Synchronizes this child shard with its parent's token.
|
|
195
|
+
*
|
|
196
|
+
* @remarks
|
|
197
|
+
* This method keeps the child's allocation stride and does not finalize ID creation ranges.
|
|
198
|
+
* Apply creation ranges from {@link IIdCompressorCore.events} separately, in order.
|
|
199
|
+
*
|
|
200
|
+
* @param token - Synchronization token obtained from this child's parent.
|
|
201
|
+
* @throws If the token names another child or contains invalid state.
|
|
202
|
+
* @throws `TypeError` if the token's generation count is not a nonnegative safe integer.
|
|
203
|
+
*/
|
|
204
|
+
synchronizeWithParent(token: ParentShardSynchronizationToken): void;
|
|
205
|
+
/**
|
|
206
|
+
* Returns undefined if this compressor is not part of a shard group, and otherwise returns a synchronization token for this shard
|
|
207
|
+
* that can be used when calling {@link IIdCompressorCore.synchronizeWithShard}. This does NOT dispose the shard.
|
|
208
|
+
*
|
|
209
|
+
* @returns The sync token if this compressor is part of a sharded group, otherwise undefined.
|
|
210
|
+
* The returned token is serializable and can be passed across marshaling boundaries. This token can be used to synchronize a
|
|
211
|
+
* parent with this compressor by calling {@link IIdCompressorCore.synchronizeWithShard}.
|
|
212
|
+
* Unlike {@link IIdCompressorCore.disposeShard}, this is non-destructive and may be called on a shard that still has active
|
|
213
|
+
* child shards, which allows an intermediate shard to propagate its progress upward to its own parent.
|
|
214
|
+
* @throws If this compressor is the root of the shard tree (only non-root shards can produce a token).
|
|
215
|
+
*/
|
|
216
|
+
getShardSyncToken(): ShardSynchronizationToken | undefined;
|
|
217
|
+
/**
|
|
218
|
+
* Returns undefined if this compressor is not part of a shard group, and otherwise disposes this shard and returns
|
|
219
|
+
* the disposal token for this compressor. If this compressor was part of a shard group, the compressor will no longer be usable.
|
|
220
|
+
*
|
|
221
|
+
* @returns The disposal token if this compressor is part of a sharded group, otherwise undefined.
|
|
222
|
+
* The returned token is serializable and can be passed across marshaling boundaries. Passing it to
|
|
223
|
+
* {@link IIdCompressorCore.synchronizeWithShard} on the parent reclaims this shard's subset of the ID space.
|
|
224
|
+
* @throws If this shard has active child shards.
|
|
225
|
+
* This means that a shard tree must be disposed from the leaves upwards.
|
|
226
|
+
*/
|
|
227
|
+
disposeShard(): ShardSynchronizationToken | undefined;
|
|
228
|
+
/**
|
|
229
|
+
* Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `deserializeIdCompressor()`.
|
|
131
230
|
* This includes finalized state as well as un-finalized state and is therefore suitable for use in offline scenarios.
|
|
132
231
|
*/
|
|
133
232
|
serialize(withSession: true): SerializedIdCompressorWithOngoingSession;
|
|
134
233
|
/**
|
|
135
|
-
* Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `
|
|
234
|
+
* Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `deserializeIdCompressor()`.
|
|
136
235
|
* This only includes finalized state and is therefore suitable for use in summaries.
|
|
137
236
|
*/
|
|
138
237
|
serialize(withSession: false): SerializedIdCompressorWithNoSession;
|
|
139
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* The serialized state of a new child compressor and its initial synchronization token.
|
|
241
|
+
*
|
|
242
|
+
* @sealed
|
|
243
|
+
* @internal
|
|
244
|
+
*/
|
|
245
|
+
export interface SerializedIdCompressorShard {
|
|
246
|
+
/**
|
|
247
|
+
* Serialized child state that can be rehydrated across a marshalling boundary.
|
|
248
|
+
*/
|
|
249
|
+
readonly serialized: SerializedIdCompressorWithOngoingSession;
|
|
250
|
+
/**
|
|
251
|
+
* Initial, non-disposing token for this child shard.
|
|
252
|
+
*
|
|
253
|
+
* @remarks
|
|
254
|
+
* After the child generates IDs, obtain its updated token before synchronizing
|
|
255
|
+
* with the parent or reclaiming the child's ID space.
|
|
256
|
+
*
|
|
257
|
+
* This can be used to immediately dispose of a new shard if it is not used.
|
|
258
|
+
*/
|
|
259
|
+
readonly syncToken: ShardSynchronizationToken;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* State shared by shard synchronization tokens.
|
|
263
|
+
* @remarks
|
|
264
|
+
* Treat these tokens as opaque: the compressor owns their interpretation and may add state in the future.
|
|
265
|
+
* The {@link ShardToken.disposed} flag distinguishes ordinary synchronization from shard reclamation.
|
|
266
|
+
* The branded {@link ShardSynchronizationToken} is the concrete child-to-parent token.
|
|
267
|
+
* @internal
|
|
268
|
+
*/
|
|
269
|
+
export interface ShardToken {
|
|
270
|
+
/**
|
|
271
|
+
* The sending compressor's generation count, which can include backfilled positions
|
|
272
|
+
* rather than only IDs it generated.
|
|
273
|
+
* @remarks Must be a nonnegative safe integer.
|
|
274
|
+
*/
|
|
275
|
+
localGenCount: number;
|
|
276
|
+
/**
|
|
277
|
+
* Identifies the child shard involved in synchronization with its parent.
|
|
278
|
+
*/
|
|
279
|
+
shardId: SessionId;
|
|
280
|
+
/**
|
|
281
|
+
* Whether this token also signals disposal of the originating shard. When `true`, passing the token to
|
|
282
|
+
* {@link IIdCompressorCore.synchronizeWithShard} reclaims the shard's ID space in addition to synchronizing.
|
|
283
|
+
* Produced as `false` by {@link IIdCompressorCore.getShardSyncToken} and `true` by {@link IIdCompressorCore.disposeShard}.
|
|
284
|
+
*/
|
|
285
|
+
disposed: boolean;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* A {@link ShardToken} that identifies a shard for synchronization (and, when {@link ShardToken.disposed} is `true`,
|
|
289
|
+
* reclamation) via {@link IIdCompressorCore.synchronizeWithShard}.
|
|
290
|
+
* @internal
|
|
291
|
+
*/
|
|
292
|
+
export type ShardSynchronizationToken = ShardToken & {
|
|
293
|
+
readonly ShardSynchronizationToken: "c79724e1-9103-4415-95b5-bebb932be404";
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* A synchronization token from a parent to one of its active child shards.
|
|
297
|
+
* @remarks
|
|
298
|
+
* Unlike the child-to-parent {@link ShardSynchronizationToken}, this token cannot dispose a shard.
|
|
299
|
+
* It does not finalize ID creation ranges.
|
|
300
|
+
* @internal
|
|
301
|
+
*/
|
|
302
|
+
export interface ParentShardSynchronizationToken extends Readonly<Omit<ShardToken, "disposed">> {
|
|
303
|
+
/** Identifies the parent-to-child token across a message boundary. */
|
|
304
|
+
readonly type: "parentIdSpaceShardSyncToken";
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Events for finalized ID creation ranges.
|
|
308
|
+
* @internal
|
|
309
|
+
*/
|
|
310
|
+
export interface IdCompressorEvents {
|
|
311
|
+
/** Reports a range after finalization succeeds. */
|
|
312
|
+
rangeFinalized: (range: IdCreationRange) => void;
|
|
313
|
+
}
|
|
140
314
|
/**
|
|
141
315
|
* A distributed UUID generator and compressor.
|
|
316
|
+
*
|
|
317
|
+
* @remarks
|
|
142
318
|
* `IdCompressor` offers the ability to generate arbitrary non-colliding v4 UUIDs, called stable IDs, while compressing them into small integers
|
|
143
319
|
* for efficient storage and transmission. It also provides the ability to decompress these integers back into their original UUIDs.
|
|
144
320
|
*
|
|
145
321
|
* The compressor is designed to be used in a distributed environment, where multiple clients may be generating IDs concurrently.
|
|
146
|
-
* IDs generated by a compressor, via calls to
|
|
322
|
+
* IDs generated by a compressor, via calls to {@link IIdCompressor.generateCompressedId}, are created in the compressor's "local" session.
|
|
147
323
|
* These IDs are unique within the session, but may not be unique across sessions. In the context of Fluid, the scope of a session
|
|
148
324
|
* is the same as the scope of a container. This means that anytime IDs are transferred between sessions, they must be translated
|
|
149
325
|
* through a process called "normalization":
|
|
326
|
+
*
|
|
150
327
|
* - The scope of a local session (in which local IDs are unique) is called the "session space" of the client.
|
|
151
328
|
* - The context of persisted state (in Fluid, this is the context of ops and summaries) is called "op space".
|
|
152
|
-
* -
|
|
329
|
+
* - {@link SessionSpaceCompressedId | SessionSpaceCompressedIds}, generated via calls to {@link IIdCompressor.generateCompressedId}, should NEVER be serialized directly.
|
|
153
330
|
* In Fluid, this means they should not be included in ops or summaries.
|
|
154
331
|
* - Before serialization, IDs must be normalized to op space to ensure they are interpretable by other clients.
|
|
155
332
|
* - Upon receipt, IDs should be normalized back to session space before use.
|
|
156
333
|
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* ### Client A (Sender)
|
|
334
|
+
* @example
|
|
160
335
|
*
|
|
336
|
+
* Client A (Sender)
|
|
161
337
|
* ```typescript
|
|
162
338
|
* // Generate several local IDs
|
|
163
339
|
* const localId1 = idCompressor.generateCompressedId();
|
|
@@ -177,7 +353,7 @@ export interface IIdCompressorCore {
|
|
|
177
353
|
* };
|
|
178
354
|
* ```
|
|
179
355
|
*
|
|
180
|
-
*
|
|
356
|
+
* Client B (Receiver)
|
|
181
357
|
*
|
|
182
358
|
* ```typescript
|
|
183
359
|
* // Receive the message from Client A
|
|
@@ -198,48 +374,57 @@ export interface IIdCompressor {
|
|
|
198
374
|
localSessionId: SessionId;
|
|
199
375
|
/**
|
|
200
376
|
* Generates a new compressed ID.
|
|
201
|
-
*
|
|
377
|
+
*
|
|
378
|
+
* @remarks The returned ID is in session space and should not be serialized directly.
|
|
379
|
+
* @see {@link IIdCompressor} for more details.
|
|
202
380
|
* @returns A new local ID in session space.
|
|
203
381
|
*/
|
|
204
382
|
generateCompressedId(): SessionSpaceCompressedId;
|
|
205
383
|
/**
|
|
206
384
|
* Generates a new ID that is guaranteed to be unique across all sessions known to this compressor without the need for any
|
|
207
|
-
* normalization.
|
|
385
|
+
* normalization.
|
|
386
|
+
*
|
|
387
|
+
* @remarks The returned ID is not guaranteed to be a compressed ID (small number); it may be a stable ID (UUID string).
|
|
208
388
|
* In Fluid, the likelihood of generating the bulkier stable ID is dictated by network conditions and is highly probable in
|
|
209
389
|
* scenarios such as offline. This is still useful for use cases where simplicity is more important than performance and
|
|
210
390
|
* this approach will often be superior to generating a UUID.
|
|
211
|
-
*
|
|
212
|
-
*
|
|
391
|
+
*
|
|
392
|
+
* If small numbers are a requirement, {@link IIdCompressor.generateCompressedId} and normalization should be used instead.
|
|
393
|
+
* @see {@link IIdCompressor} for more details.
|
|
213
394
|
* @returns A new local ID in session space.
|
|
214
395
|
*/
|
|
215
396
|
generateDocumentUniqueId(): (SessionSpaceCompressedId & OpSpaceCompressedId) | StableId;
|
|
216
397
|
/**
|
|
217
398
|
* Normalizes a session space ID into op space.
|
|
218
|
-
*
|
|
399
|
+
*
|
|
400
|
+
* @remarks The returned ID is in op space and can be safely serialized.
|
|
219
401
|
* However, it should be {@link IIdCompressor.normalizeToSessionSpace|normalized back to session space} before use.
|
|
402
|
+
*
|
|
220
403
|
* @param id - The local ID to normalize.
|
|
221
404
|
* @returns The ID in op space.
|
|
222
405
|
*/
|
|
223
406
|
normalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId;
|
|
224
407
|
/**
|
|
225
408
|
* Normalizes an ID into session space.
|
|
226
|
-
*
|
|
227
|
-
* @param originSessionId - The {@link IIdCompressor.localSessionId} of the compressor which {@link IIdCompressor.normalizeToOpSpace|encoded} `id`.
|
|
228
|
-
* This might not be the client that created `id` in the first place, but rather the client that serialized it.
|
|
229
|
-
* This is an important distinction in the case of a reference, where a client might refer to an ID created by another client.
|
|
230
|
-
* @returns The session-space ID in the local session corresponding to `id`.
|
|
409
|
+
*
|
|
231
410
|
* @remarks
|
|
232
411
|
* For this to be valid, `originSessionId` must be the {@link IIdCompressor.localSessionId} of the compressor that normalized `id` to op space.
|
|
233
|
-
* Additionally, this compressor must know about the relevant {@link IdCreationRange} from the encoding
|
|
412
|
+
* Additionally, this compressor must know about the relevant {@link IdCreationRange} from the encoding {@link IIdCompressor}.
|
|
234
413
|
* This can be accomplished using creating the compressor using {@link (deserializeIdCompressor:1)},
|
|
235
414
|
* or relying on something (like the Fluid runtime) to synchronize the range information using internal mechanisms.
|
|
236
415
|
*
|
|
237
|
-
* The {@link IIdCompressor} provided by the Fluid Framework runtime will automatically track {@link IdCreationRange| Id Creation Ranges} (via its own ops) from other clients in the container.
|
|
416
|
+
* The {@link IIdCompressor} provided by the Fluid Framework runtime will automatically track {@link IdCreationRange | Id Creation Ranges} (via its own ops) from other clients in the container.
|
|
238
417
|
* It is up to the user of the {@link IIdCompressor} to ensure the relevant session information is available,
|
|
239
418
|
* either by depending on the originating client's session ID for ops, or by explicitly storing it alongside the data containing the {@link OpSpaceCompressedId}.
|
|
240
419
|
*
|
|
241
420
|
* Note that attachment summaries, while they are ops, are also interpreted as summaries without the context of their originating session,
|
|
242
421
|
* so they should explicitly include session IDs if required to decode them just like regular summaries and exported data.
|
|
422
|
+
*
|
|
423
|
+
* @param id - The ID to normalize.
|
|
424
|
+
* @param originSessionId - The {@link IIdCompressor.localSessionId} of the compressor which {@link IIdCompressor.normalizeToOpSpace|encoded} `id`.
|
|
425
|
+
* This might not be the client that created `id` in the first place, but rather the client that serialized it.
|
|
426
|
+
* This is an important distinction in the case of a reference, where a client might refer to an ID created by another client.
|
|
427
|
+
* @returns The session-space ID in the local session corresponding to `id`.
|
|
243
428
|
*/
|
|
244
429
|
normalizeToSessionSpace(id: OpSpaceCompressedId, originSessionId: SessionId): SessionSpaceCompressedId;
|
|
245
430
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idCompressor.d.ts","sourceRoot":"","sources":["../../src/types/idCompressor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACX,mBAAmB,EACnB,SAAS,EACT,wBAAwB,EACxB,QAAQ,EACR,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EACX,eAAe,EACf,mCAAmC,EACnC,wCAAwC,EACxC,MAAM,4BAA4B,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,MAAM,WAAW,iBAAiB;IACjC
|
|
1
|
+
{"version":3,"file":"idCompressor.d.ts","sourceRoot":"","sources":["../../src/types/idCompressor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACX,mBAAmB,EACnB,SAAS,EACT,wBAAwB,EACxB,QAAQ,EACR,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,KAAK,EACX,eAAe,EACf,mCAAmC,EACnC,wCAAwC,EACxC,MAAM,4BAA4B,CAAC;AAEpC;;;GAGG;AACH,eAAO,MAAM,oBAAoB;IAChC;;OAEG;;IAEH;;OAEG;;CAEM,CAAC;AAEX;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAC/B,CAAC,OAAO,oBAAoB,CAAC,CAAC,MAAM,OAAO,oBAAoB,CAAC,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,MAAM,WAAW,iBAAiB;IACjC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,kBAAkB,CAAC,CAAC;IAEhD;;;;;;;;;OASG;IACH,qBAAqB,IAAI,eAAe,CAAC;IAEzC;;;;;;;OAOG;IACH,4BAA4B,IAAI,eAAe,CAAC;IAEhD;;;;;;;;;;;;;OAaG;IACH,6BAA6B,IAAI,IAAI,CAAC;IAEtC;;;OAGG;IACH,qBAAqB,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI,CAAC;IAEpD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,iBAAiB,CAAC,cAAc,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,IAAI,GAAG,IAAI,CAAC;IAErF;;;;;;;;;;OAUG;IACH,KAAK,CAAC,aAAa,EAAE,MAAM,GAAG,2BAA2B,EAAE,CAAC;IAE5D;;;;;;;;;;;;OAYG;IACH,oBAAoB,CAAC,SAAS,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAEjE;;;;;;;;;OASG;IACH,sBAAsB,CACrB,UAAU,EAAE,yBAAyB,GACnC,+BAA+B,CAAC;IAEnC;;;;;;;;;;OAUG;IACH,qBAAqB,CAAC,KAAK,EAAE,+BAA+B,GAAG,IAAI,CAAC;IAEpE;;;;;;;;;;OAUG;IACH,iBAAiB,IAAI,yBAAyB,GAAG,SAAS,CAAC;IAE3D;;;;;;;;;OASG;IACH,YAAY,IAAI,yBAAyB,GAAG,SAAS,CAAC;IAEtD;;;OAGG;IACH,SAAS,CAAC,WAAW,EAAE,IAAI,GAAG,wCAAwC,CAAC;IAEvE;;;OAGG;IACH,SAAS,CAAC,WAAW,EAAE,KAAK,GAAG,mCAAmC,CAAC;CACnE;AAED;;;;;GAKG;AACH,MAAM,WAAW,2BAA2B;IAC3C;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,wCAAwC,CAAC;IAE9D;;;;;;;;OAQG;IACH,QAAQ,CAAC,SAAS,EAAE,yBAAyB,CAAC;CAC9C;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU;IAC1B;;;;OAIG;IACH,aAAa,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,OAAO,EAAE,SAAS,CAAC;IAEnB;;;;OAIG;IACH,QAAQ,EAAE,OAAO,CAAC;CAClB;AAED;;;;GAIG;AACH,MAAM,MAAM,yBAAyB,GAAG,UAAU,GAAG;IACpD,QAAQ,CAAC,yBAAyB,EAAE,sCAAsC,CAAC;CAC3E,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,WAAW,+BAChB,SAAQ,QAAQ,CAAC,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IAC9C,sEAAsE;IACtE,QAAQ,CAAC,IAAI,EAAE,6BAA6B,CAAC;CAC7C;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IAClC,mDAAmD;IACnD,cAAc,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;CACjD;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,MAAM,WAAW,aAAa;IAC7B;;OAEG;IACH,cAAc,EAAE,SAAS,CAAC;IAE1B;;;;;;OAMG;IACH,oBAAoB,IAAI,wBAAwB,CAAC;IAEjD;;;;;;;;;;;;OAYG;IACH,wBAAwB,IAAI,CAAC,wBAAwB,GAAG,mBAAmB,CAAC,GAAG,QAAQ,CAAC;IAExF;;;;;;;;OAQG;IACH,kBAAkB,CAAC,EAAE,EAAE,wBAAwB,GAAG,mBAAmB,CAAC;IAEtE;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,uBAAuB,CACtB,EAAE,EAAE,mBAAmB,EACvB,eAAe,EAAE,SAAS,GACxB,wBAAwB,CAAC;IAE5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACH,wCAAwC,CACvC,EAAE,EAAE,mBAAmB,GACrB,wBAAwB,GAAG,SAAS,CAAC;IAExC;;;;;OAKG;IACH,UAAU,CAAC,EAAE,EAAE,wBAAwB,GAAG,QAAQ,CAAC;IAEnD;;;;;OAKG;IACH,UAAU,CAAC,YAAY,EAAE,QAAQ,GAAG,wBAAwB,CAAC;IAE7D;;;;OAIG;IACH,aAAa,CAAC,YAAY,EAAE,QAAQ,GAAG,wBAAwB,GAAG,SAAS,CAAC;CAC5E"}
|
|
@@ -4,4 +4,19 @@
|
|
|
4
4
|
* Licensed under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.SerializationVersion = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Serialization format versions for IdCompressor.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
exports.SerializationVersion = {
|
|
13
|
+
/**
|
|
14
|
+
* Base format without sharding support
|
|
15
|
+
*/
|
|
16
|
+
V2: 2,
|
|
17
|
+
/**
|
|
18
|
+
* Adds optional sharding state
|
|
19
|
+
*/
|
|
20
|
+
V3: 3,
|
|
21
|
+
};
|
|
7
22
|
//# sourceMappingURL=idCompressor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idCompressor.js","sourceRoot":"","sources":["../../src/types/idCompressor.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tOpSpaceCompressedId,\n\tSessionId,\n\tSessionSpaceCompressedId,\n\tStableId,\n} from \"./identifiers.js\";\nimport type {\n\tIdCreationRange,\n\tSerializedIdCompressorWithNoSession,\n\tSerializedIdCompressorWithOngoingSession,\n} from \"./persisted-types/index.js\";\n\n/**\n * A distributed UUID generator and compressor.\n *\n * Generates arbitrary non-colliding v4 UUIDs, called stable IDs, for multiple \"sessions\" (which can be distributed across the network),\n * providing each session with the ability to map these UUIDs to `numbers`.\n *\n * A session is a unique identifier that denotes a single compressor. New IDs are created through a single compressor API\n * which should then be sent in ranges to the server for total ordering (and are subsequently relayed to other clients). When a new ID is\n * created it is said to be created by the compressor's \"local\" session.\n *\n * For each stable ID created, two numeric IDs are provided by the compressor:\n *\n * 1. A session-local ID, which is stable for the lifetime of the session (which could be longer than that of the compressor object, as it may\n * be serialized for offline usage). Available as soon as the stable ID is allocated. These IDs are session-unique and are thus only\n * safely usable within the scope of the compressor that created it.\n *\n * 2. A final ID, which is stable across serialization and deserialization of an IdCompressor. Available as soon as the range containing\n * the corresponding session-local ID is totally ordered (via consensus) with respect to other sessions' allocations.\n * Final IDs are known to and publicly usable by any compressor that has received them.\n *\n * Compressors will allocate UUIDs in non-random ways to reduce entropy allowing for optimized storage of the data needed\n * to map the UUIDs to the numbers.\n *\n * The following invariants are upheld by IdCompressor:\n *\n * 1. Session-local IDs will always decompress to the same UUIDs for the lifetime of the session.\n *\n * 2. Final IDs will always decompress to the same UUIDs.\n *\n * 3. After a server-processed range of session-local IDs (from any session) is received by a compressor, any of those session-local IDs may be\n * translated by the compressor into the corresponding final ID. For any given session-local ID, this translation will always yield the\n * same final ID.\n *\n * 4. A UUID will always compress into the same session-local ID for the lifetime of the session.\n *\n * Session-local IDs are sent across the wire in efficiently-represented ranges. These ranges are created by querying the compressor, and *must*\n * be ordered (i.e. sent to the server) in the order they are created in order to preserve the above invariants.\n *\n * Session-local IDs can be used immediately after creation, but will eventually (after being sequenced) have a corresponding final ID. This\n * could make reasoning about equality of those two forms difficult. For example, if a cache is keyed off of a\n * session-local ID but is later queried using the final ID (which is semantically equal, as it decompresses to the same UUID/string) it will\n * produce a cache miss. In order to make using collections of both remotely created and locally created IDs easy, regardless of whether the\n * session-local IDs have been finalized, the compressor defines two \"spaces\" of IDs:\n *\n * 1. Session space: in this space, all IDs are normalized to their \"most local form\". This means that all IDs created by the local session\n * will be in local form, regardless of if they have been finalized. Remotely created IDs, which could only have been received after\n * finalizing and will never have a local form for the compressor, will of course be final IDs. This space should be used with consumer APIs\n * and data structures, as the lifetime of the IDs is guaranteed to be the same as the compressor object. Care must be taken to not use\n * these IDs across compressor objects, as the local IDs are specific to the compressor that created them.\n *\n * 2. Op space: in this space, all IDs are normalized to their \"most final form\". This means that all IDs except session-local IDs that\n * have not yet been finalized will be in final ID form. This space is useful for serialization in ops (e.g. references), as other clients\n * that receive them need not do any work to normalize them to *their* session-space in the common case. Note that IDs in op space may move\n * out of Op space over time, namely, when a session-local ID in this space becomes finalized, and thereafter has a \"more final form\".\n * Consequentially, it may be useful to restrict parameters of a persisted type to this space (to optimize perf), but it is potentially\n * incorrect to use this type for a runtime variable. This is an asymmetry that does not affect session space, as local IDs are always as\n * \"local as possible\".\n *\n * These two spaces naturally define a rule: consumers of compressed IDs should use session-space IDs, but serialized forms such as ops\n * should use op-space IDs.\n *\n * @internal\n */\nexport interface IIdCompressorCore {\n\t/**\n\t * Returns a range of IDs created by this session in a format for sending to the server for finalizing.\n\t * The range will include all IDs generated via calls to `generateCompressedId` since the last time a\n\t * range was taken (via this method or `takeUnfinalizedCreationRange`).\n\t * @returns the range of IDs, which may be empty. This range must be sent to the server for ordering before\n\t * it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.\n\t */\n\ttakeNextCreationRange(): IdCreationRange;\n\n\t/**\n\t * Returns a range of IDs created by this session in a format for sending to the server for finalizing.\n\t * The range will include all unfinalized IDs generated via calls to `generateCompressedId`.\n\t * @returns the range of IDs, which may be empty. This range must be sent to the server for ordering before\n\t * it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.\n\t * Note: after finalizing the range returned by this method, finalizing any ranges that had been previously taken\n\t * will result in an error.\n\t */\n\ttakeUnfinalizedCreationRange(): IdCreationRange;\n\n\t/**\n\t * Resets the next creation range to include all unfinalized IDs.\n\t *\n\t * @remarks\n\t * IMPORTANT: This must only be called if it's CERTAIN that the unfinalized range will never be finalized as-is (e.g. by in-flight ops).\n\t *\n\t * After calling this, the next call to {@link IIdCompressorCore.takeNextCreationRange} will produce a range\n\t * covering all unfinalized IDs (equivalent to what {@link IIdCompressorCore.takeUnfinalizedCreationRange} would\n\t * have returned) plus any IDs generated after this call.\n\t *\n\t * Unlike {@link IIdCompressorCore.takeUnfinalizedCreationRange}, this method does not produce or return a range,\n\t * and does not advance the internal range counter. It is useful when the caller wants to\n\t * defer the actual range submission to the next natural {@link IIdCompressorCore.takeNextCreationRange} call.\n\t */\n\tresetUnfinalizedCreationRange(): void;\n\n\t/**\n\t * Finalizes the supplied range of IDs (which may be from either a remote or local session).\n\t * @param range - the range of session-local IDs to finalize.\n\t */\n\tfinalizeCreationRange(range: IdCreationRange): void;\n\n\t/**\n\t * Run a callback that is performed from the perspective of a special \"ghost\" session.\n\t * Any ids generated by this session will be immediately finalized on the local client as if\n\t * they were created by a remote client with `ghostSessionId`.\n\t *\n\t * *WARNING:* This API requires an external consensus mechanism to safely use:\n\t * In an attached container (i.e. multiple clients may have the document loaded), all clients must guarantee that:\n\t * - They invoke this API starting from the same finalized creation ranges\n\t * - This API is invoked with the same ghost session id\n\t * - `ghostSessionCallback` deterministically mints the same number of ids on each client within the ghost session\n\t * Failure to meet these requirements will result in divergence across clients and eventual consistency errors.\n\t * While the ghost session callback is running, IdCompressor does not support serialization.\n\t * @remarks This API is primarily intended for data migration scenarios which are able to deterministically transform\n\t * data in some format into data in a new format.\n\t * The first requirement (that all clients must invoke the API with the same finalized creation ranges) is guaranteed\n\t * for this scenario because the data transformation callback occurs at a specific ack within the op stream on all\n\t * clients, and clients don't finalize creation ranges for local changes they might have at this point in time.\n\t * @param ghostSessionId - The session id that minted ids generated within `ghostSessionCallback` should be attributed to.\n\t * @param ghostSessionCallback - Callback which mints ids attributed to the ghost session.\n\t */\n\tbeginGhostSession(ghostSessionId: SessionId, ghostSessionCallback: () => void): void;\n\n\t/**\n\t * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `IdCompressor.deserialize()`.\n\t * This includes finalized state as well as un-finalized state and is therefore suitable for use in offline scenarios.\n\t */\n\tserialize(withSession: true): SerializedIdCompressorWithOngoingSession;\n\n\t/**\n\t * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `IdCompressor.deserialize()`.\n\t * This only includes finalized state and is therefore suitable for use in summaries.\n\t */\n\tserialize(withSession: false): SerializedIdCompressorWithNoSession;\n}\n\n/**\n * A distributed UUID generator and compressor.\n * `IdCompressor` offers the ability to generate arbitrary non-colliding v4 UUIDs, called stable IDs, while compressing them into small integers\n * for efficient storage and transmission. It also provides the ability to decompress these integers back into their original UUIDs.\n *\n * The compressor is designed to be used in a distributed environment, where multiple clients may be generating IDs concurrently.\n * IDs generated by a compressor, via calls to `generateCompressedId`, are created in the compressor's \"local\" session.\n * These IDs are unique within the session, but may not be unique across sessions. In the context of Fluid, the scope of a session\n * is the same as the scope of a container. This means that anytime IDs are transferred between sessions, they must be translated\n * through a process called \"normalization\":\n * - The scope of a local session (in which local IDs are unique) is called the \"session space\" of the client.\n * - The context of persisted state (in Fluid, this is the context of ops and summaries) is called \"op space\".\n * - `SessionSpaceCompressedId`s, generated via calls to `generateCompressedId`, should NEVER be serialized directly.\n * In Fluid, this means they should not be included in ops or summaries.\n * - Before serialization, IDs must be normalized to op space to ensure they are interpretable by other clients.\n * - Upon receipt, IDs should be normalized back to session space before use.\n *\n * Example Usage:\n *\n * ### Client A (Sender)\n *\n * ```typescript\n * // Generate several local IDs\n * const localId1 = idCompressor.generateCompressedId();\n * const localId2 = idCompressor.generateCompressedId();\n * const localId3 = idCompressor.generateCompressedId();\n *\n * // Normalize these IDs to op space for inclusion in a message\n * const opSpaceId1 = idCompressor.normalizeToOpSpace(localId1);\n * const opSpaceId2 = idCompressor.normalizeToOpSpace(localId2);\n * const opSpaceId3 = idCompressor.normalizeToOpSpace(localId3);\n *\n * // Create and send a message containing these op space IDs along with the sender's session ID\n * // In Fluid, this would be an op or summary\n * const message = {\n * sessionID: idCompressor.localSessionId,\n * ids: [opSpaceId1, opSpaceId2, opSpaceId3]\n * };\n * ```\n *\n * ### Client B (Receiver)\n *\n * ```typescript\n * // Receive the message from Client A\n * const receivedMessage = ...; // In Fluid, this would be an op or summary\n *\n * // Normalize the received IDs back to session space, utilizing the sender's session ID\n * const sessionSpaceId1 = idCompressor.normalizeToSessionSpace(receivedMessage.ids[0], receivedMessage.sessionID);\n * const sessionSpaceId2 = idCompressor.normalizeToSessionSpace(receivedMessage.ids[1], receivedMessage.sessionID);\n * const sessionSpaceId3 = idCompressor.normalizeToSessionSpace(receivedMessage.ids[2], receivedMessage.sessionID);\n * ```\n * @public\n * @sealed\n */\nexport interface IIdCompressor {\n\t/**\n\t * The local session ID.\n\t */\n\tlocalSessionId: SessionId;\n\n\t/**\n\t * Generates a new compressed ID.\n\t * The returned ID is in session space and should not be serialized directly. See `IIdCompressor` for more details.\n\t * @returns A new local ID in session space.\n\t */\n\tgenerateCompressedId(): SessionSpaceCompressedId;\n\n\t/**\n\t * Generates a new ID that is guaranteed to be unique across all sessions known to this compressor without the need for any\n\t * normalization. The returned ID is not guaranteed to be a compressed ID (small number); it may be a stable ID (UUID string).\n\t * In Fluid, the likelihood of generating the bulkier stable ID is dictated by network conditions and is highly probable in\n\t * scenarios such as offline. This is still useful for use cases where simplicity is more important than performance and\n\t * this approach will often be superior to generating a UUID.\n\t * If small numbers are a requirement, `generateCompressedId` and normalization should be used instead.\n\t * See `IIdCompressor` for more details.\n\t * @returns A new local ID in session space.\n\t */\n\tgenerateDocumentUniqueId(): (SessionSpaceCompressedId & OpSpaceCompressedId) | StableId;\n\n\t/**\n\t * Normalizes a session space ID into op space.\n\t * The returned ID is in op space and can be safely serialized.\n\t * However, it should be {@link IIdCompressor.normalizeToSessionSpace|normalized back to session space} before use.\n\t * @param id - The local ID to normalize.\n\t * @returns The ID in op space.\n\t */\n\tnormalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId;\n\n\t/**\n\t * Normalizes an ID into session space.\n\t * @param id - The ID to normalize.\n\t * @param originSessionId - The {@link IIdCompressor.localSessionId} of the compressor which {@link IIdCompressor.normalizeToOpSpace|encoded} `id`.\n\t * This might not be the client that created `id` in the first place, but rather the client that serialized it.\n\t * This is an important distinction in the case of a reference, where a client might refer to an ID created by another client.\n\t * @returns The session-space ID in the local session corresponding to `id`.\n\t * @remarks\n\t * For this to be valid, `originSessionId` must be the {@link IIdCompressor.localSessionId} of the compressor that normalized `id` to op space.\n\t * Additionally, this compressor must know about the relevant {@link IdCreationRange} from the encoding `IIdCompressor`.\n\t * This can be accomplished using creating the compressor using {@link (deserializeIdCompressor:1)},\n\t * or relying on something (like the Fluid runtime) to synchronize the range information using internal mechanisms.\n\t *\n\t * The {@link IIdCompressor} provided by the Fluid Framework runtime will automatically track {@link IdCreationRange| Id Creation Ranges} (via its own ops) from other clients in the container.\n\t * It is up to the user of the {@link IIdCompressor} to ensure the relevant session information is available,\n\t * either by depending on the originating client's session ID for ops, or by explicitly storing it alongside the data containing the {@link OpSpaceCompressedId}.\n\t *\n\t * Note that attachment summaries, while they are ops, are also interpreted as summaries without the context of their originating session,\n\t * so they should explicitly include session IDs if required to decode them just like regular summaries and exported data.\n\t */\n\tnormalizeToSessionSpace(\n\t\tid: OpSpaceCompressedId,\n\t\toriginSessionId: SessionId,\n\t): SessionSpaceCompressedId;\n\n\t/**\n\t * Attempts to normalize an op-space ID into session space without an originator session id.\n\t * This mainly exists for data recovery purposes, when the originator session id was lost.\n\t * When possible, use {@link IIdCompressor.normalizeToSessionSpace} instead.\n\t *\n\t * @remarks\n\t * When the id is a final ID, this will return the properly translated session-space ID,\n\t * or throw if it detects that the final id is not valid in this compressor.\n\t *\n\t * Returns `undefined` if `id` is a non-final op-space ID — those are local to the\n\t * originating session and require its session id to resolve. Callers which have the\n\t * originating session id should call {@link IIdCompressor.normalizeToSessionSpace} instead.\n\t *\n\t * @param id - The ID to normalize.\n\t * @returns The session-space ID corresponding to `id`, or `undefined` if `id` is\n\t * non-final and therefore requires an originator session id to resolve.\n\t * @throws If `id` is final but is not known to this compressor (i.e. it refers to\n\t * a final id that has never been observed as finalized).\n\t * Note that it is possible (even likely) for a finalized id from another id compressor to pass this check,\n\t * and return an incorrect session-space ID.\n\t * This error is thrown only in cases where it is possible to detect the input is invalid,\n\t * and intended to help with catching bugs where invalid identifiers are being provided.\n\t * It cannot be relied upon for validation.\n\t *\n\t * @privateRemarks\n\t * Currently this is only used for data recovery in the case of lost session ids,\n\t * but we could theoretically provide an API to determine if the session id is required when encoding data (some IDs were non-final):\n\t * in such a setup, this API, and/or a version of normalizeToSessionSpace where the session id is optional,\n\t * could be used for a regular non-recovery use-case.\n\t */\n\ttryNormalizeToSessionSpaceWithoutSession(\n\t\tid: OpSpaceCompressedId,\n\t): SessionSpaceCompressedId | undefined;\n\n\t/**\n\t * Decompresses a previously compressed ID into a UUID.\n\t * @param id - The compressed ID to be decompressed.\n\t * @returns The UUID associated with the compressed ID.\n\t * @throws If the ID was not generated by any session known to this compressor.\n\t */\n\tdecompress(id: SessionSpaceCompressedId): StableId;\n\n\t/**\n\t * Recompresses a UUID.\n\t * @param uncompressed - The UUID to recompress.\n\t * @returns The `CompressedId` associated with `uncompressed`.\n\t * @throws If the UUID was not generated by any session known to this compressor.\n\t */\n\trecompress(uncompressed: StableId): SessionSpaceCompressedId;\n\n\t/**\n\t * Attempts to recompress a UUID.\n\t * @param uncompressed - The UUID to recompress.\n\t * @returns The `CompressedId` associated with `uncompressed` or undefined if the UUID was not generated by any session known to this compressor.\n\t */\n\ttryRecompress(uncompressed: StableId): SessionSpaceCompressedId | undefined;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"idCompressor.js","sourceRoot":"","sources":["../../src/types/idCompressor.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAeH;;;GAGG;AACU,QAAA,oBAAoB,GAAG;IACnC;;OAEG;IACH,EAAE,EAAE,CAAC;IACL;;OAEG;IACH,EAAE,EAAE,CAAC;CACI,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tOpSpaceCompressedId,\n\tSessionId,\n\tSessionSpaceCompressedId,\n\tStableId,\n} from \"./identifiers.js\";\nimport type { Listenable } from \"@fluidframework/core-interfaces\";\nimport type {\n\tIdCreationRange,\n\tSerializedIdCompressorWithNoSession,\n\tSerializedIdCompressorWithOngoingSession,\n} from \"./persisted-types/index.js\";\n\n/**\n * Serialization format versions for IdCompressor.\n * @internal\n */\nexport const SerializationVersion = {\n\t/**\n\t * Base format without sharding support\n\t */\n\tV2: 2,\n\t/**\n\t * Adds optional sharding state\n\t */\n\tV3: 3,\n} as const;\n\n/**\n * Type representing valid serialization version values.\n * @internal\n */\nexport type SerializationVersion =\n\t(typeof SerializationVersion)[keyof typeof SerializationVersion];\n\n/**\n * A distributed UUID generator and compressor.\n *\n * Generates arbitrary non-colliding v4 UUIDs, called stable IDs, for multiple \"sessions\" (which can be distributed across the network),\n * providing each session with the ability to map these UUIDs to `numbers`.\n *\n * A session is a unique identifier that denotes a single compressor. New IDs are created through a single compressor API\n * which should then be sent in ranges to the server for total ordering (and are subsequently relayed to other clients). When a new ID is\n * created it is said to be created by the compressor's \"local\" session.\n *\n * For each stable ID created, two numeric IDs are provided by the compressor:\n *\n * 1. A session-local ID, which is stable for the lifetime of the session (which could be longer than that of the compressor object, as it may\n * be serialized for offline usage). Available as soon as the stable ID is allocated. These IDs are session-unique and are thus only\n * safely usable within the scope of the compressor that created it.\n *\n * 2. A final ID, which is stable across serialization and deserialization of an IdCompressor. Available as soon as the range containing\n * the corresponding session-local ID is totally ordered (via consensus) with respect to other sessions' allocations.\n * Final IDs are known to and publicly usable by any compressor that has received them.\n *\n * Compressors will allocate UUIDs in non-random ways to reduce entropy allowing for optimized storage of the data needed\n * to map the UUIDs to the numbers.\n *\n * The following invariants are upheld by IdCompressor:\n *\n * 1. Session-local IDs will always decompress to the same UUIDs for the lifetime of the session.\n *\n * 2. Final IDs will always decompress to the same UUIDs.\n *\n * 3. After a server-processed range of session-local IDs (from any session) is received by a compressor, any of those session-local IDs may be\n * translated by the compressor into the corresponding final ID. For any given session-local ID, this translation will always yield the\n * same final ID.\n *\n * 4. A UUID will always compress into the same session-local ID for the lifetime of the session.\n *\n * Session-local IDs are sent across the wire in efficiently-represented ranges. These ranges are created by querying the compressor, and *must*\n * be ordered (i.e. sent to the server) in the order they are created in order to preserve the above invariants.\n *\n * Session-local IDs can be used immediately after creation, but will eventually (after being sequenced) have a corresponding final ID. This\n * could make reasoning about equality of those two forms difficult. For example, if a cache is keyed off of a\n * session-local ID but is later queried using the final ID (which is semantically equal, as it decompresses to the same UUID/string) it will\n * produce a cache miss. In order to make using collections of both remotely created and locally created IDs easy, regardless of whether the\n * session-local IDs have been finalized, the compressor defines two \"spaces\" of IDs:\n *\n * 1. Session space: in this space, all IDs are normalized to their \"most local form\". This means that all IDs created by the local session\n * will be in local form, regardless of if they have been finalized. Remotely created IDs, which could only have been received after\n * finalizing and will never have a local form for the compressor, will of course be final IDs. This space should be used with consumer APIs\n * and data structures, as the lifetime of the IDs is guaranteed to be the same as the compressor object. Care must be taken to not use\n * these IDs across compressor objects, as the local IDs are specific to the compressor that created them.\n *\n * 2. Op space: in this space, all IDs are normalized to their \"most final form\". This means that all IDs except session-local IDs that\n * have not yet been finalized will be in final ID form. This space is useful for serialization in ops (e.g. references), as other clients\n * that receive them need not do any work to normalize them to *their* session-space in the common case. Note that IDs in op space may move\n * out of Op space over time, namely, when a session-local ID in this space becomes finalized, and thereafter has a \"more final form\".\n * Consequentially, it may be useful to restrict parameters of a persisted type to this space (to optimize perf), but it is potentially\n * incorrect to use this type for a runtime variable. This is an asymmetry that does not affect session space, as local IDs are always as\n * \"local as possible\".\n *\n * These two spaces naturally define a rule: consumers of compressed IDs should use session-space IDs, but serialized forms such as ops\n * should use op-space IDs.\n *\n * @internal\n */\nexport interface IIdCompressorCore {\n\t/**\n\t * Events emitted by the ID compressor.\n\t */\n\treadonly events: Listenable<IdCompressorEvents>;\n\n\t/**\n\t * Returns a range of IDs created by this session in a format for sending to the server for finalizing.\n\t * The range will include all IDs generated via calls to `generateCompressedId` since the last time a\n\t * range was taken (via this method or `takeUnfinalizedCreationRange`).\n\t * @returns the range of IDs, which may be empty. This range must be sent to the server for ordering before\n\t * it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.\n\t * @throws if called on a child (non-root) shard. All shards in a shard tree share a single session, and only\n\t * the root shard may finalize that session's IDs with the server; a child's IDs are instead reconciled with the\n\t * root via {@link IIdCompressorCore.synchronizeWithShard} / {@link IIdCompressorCore.disposeShard}.\n\t */\n\ttakeNextCreationRange(): IdCreationRange;\n\n\t/**\n\t * Returns a range of IDs created by this session in a format for sending to the server for finalizing.\n\t * The range will include all unfinalized IDs generated via calls to `generateCompressedId`.\n\t * @returns the range of IDs, which may be empty. This range must be sent to the server for ordering before\n\t * it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.\n\t * Note: after finalizing the range returned by this method, finalizing any ranges that had been previously taken\n\t * will result in an error.\n\t */\n\ttakeUnfinalizedCreationRange(): IdCreationRange;\n\n\t/**\n\t * Resets the next creation range to include all unfinalized IDs.\n\t *\n\t * @remarks\n\t * IMPORTANT: This must only be called if it's CERTAIN that the unfinalized range will never be finalized as-is (e.g. by in-flight ops).\n\t *\n\t * After calling this, the next call to {@link IIdCompressorCore.takeNextCreationRange} will produce a range\n\t * covering all unfinalized IDs (equivalent to what {@link IIdCompressorCore.takeUnfinalizedCreationRange} would\n\t * have returned) plus any IDs generated after this call.\n\t *\n\t * Unlike {@link IIdCompressorCore.takeUnfinalizedCreationRange}, this method does not produce or return a range,\n\t * and does not advance the internal range counter. It is useful when the caller wants to\n\t * defer the actual range submission to the next natural {@link IIdCompressorCore.takeNextCreationRange} call.\n\t */\n\tresetUnfinalizedCreationRange(): void;\n\n\t/**\n\t * Finalizes the supplied range of IDs (which may be from either a remote or local session).\n\t * @param range - the range of session-local IDs to finalize.\n\t */\n\tfinalizeCreationRange(range: IdCreationRange): void;\n\n\t/**\n\t * Run a callback that is performed from the perspective of a special \"ghost\" session.\n\t * Any ids generated by this session will be immediately finalized on the local client as if\n\t * they were created by a remote client with `ghostSessionId`.\n\t *\n\t * *WARNING:* This API requires an external consensus mechanism to safely use:\n\t * In an attached container (i.e. multiple clients may have the document loaded), all clients must guarantee that:\n\t * - They invoke this API starting from the same finalized creation ranges\n\t * - This API is invoked with the same ghost session id\n\t * - `ghostSessionCallback` deterministically mints the same number of ids on each client within the ghost session\n\t * Failure to meet these requirements will result in divergence across clients and eventual consistency errors.\n\t * While the ghost session callback is running, IdCompressor does not support serialization.\n\t * @remarks This API is primarily intended for data migration scenarios which are able to deterministically transform\n\t * data in some format into data in a new format.\n\t * The first requirement (that all clients must invoke the API with the same finalized creation ranges) is guaranteed\n\t * for this scenario because the data transformation callback occurs at a specific ack within the op stream on all\n\t * clients, and clients don't finalize creation ranges for local changes they might have at this point in time.\n\t * @param ghostSessionId - The session id that minted ids generated within `ghostSessionCallback` should be attributed to.\n\t * @param ghostSessionCallback - Callback which mints ids attributed to the ghost session.\n\t */\n\tbeginGhostSession(ghostSessionId: SessionId, ghostSessionCallback: () => void): void;\n\n\t/**\n\t * Shards the ID space of this compressor such that multiple local compressors can safely share it without colliding.\n\t * This can allow multiple local instantiations of the same compressor to safely share an ID space in scenarios where\n\t * different threads do not have access to a central ID compressor.\n\t * @param newShardCount - The number of additional different shards to split this compressor into.\n\t * Must be a positive safe integer.\n\t * @throws If `newShardCount` is not a positive safe integer or the resulting stride exceeds the sharding limit.\n\t * @returns An array of `newShardCount` {@link SerializedIdCompressorShard} objects.\n\t * Note that this method should only be needed when multiple JS runtimes are in play, as sharded compressors essentially\n\t * attempt to emulate a single static compressor and any code running in the same JS runtime can simply use statics.\n\t */\n\tshard(newShardCount: number): SerializedIdCompressorShard[];\n\n\t/**\n\t * Synchronizes `this` compressor with a child shard. Synchronization will occur for the state of the child at the time `syncToken`\n\t * was generated, meaning that `this` compressor can use/ingest IDs generated up to that point. Attempts to use IDs from a child shard\n\t * without first synchronizing will result in an exception.\n\t *\n\t * If `syncToken` is a disposal token (its {@link ShardToken.disposed} flag is `true`, as produced by\n\t * {@link IIdCompressorCore.disposeShard}), this additionally deregisters the child shard and reclaims its subset of the ID space\n\t * into `this`. Once a shard has been disposed it is no longer safe to use the compressor the token came from, and a shard tree must\n\t * be disposed from the leaves upwards.\n\t * @param syncToken - The token for the shard, obtained by calling {@link IIdCompressorCore.getShardSyncToken} (non-destructive\n\t * synchronization) or {@link IIdCompressorCore.disposeShard} (synchronization plus reclamation of the disposed shard's ID space).\n\t * @throws `TypeError` if the token's generation count is not a nonnegative safe integer.\n\t */\n\tsynchronizeWithShard(syncToken: ShardSynchronizationToken): void;\n\n\t/**\n\t * Gets a synchronization token from this compressor for an active child shard.\n\t *\n\t * @remarks\n\t * The child can apply this token without replacing its compressor.\n\t *\n\t * @param childToken - A token from the child shard that will receive this token.\n\t * @returns This parent's synchronization token for that child.\n\t * @throws If the child shard is not active on this compressor.\n\t */\n\tgetChildShardSyncToken(\n\t\tchildToken: ShardSynchronizationToken,\n\t): ParentShardSynchronizationToken;\n\n\t/**\n\t * Synchronizes this child shard with its parent's token.\n\t *\n\t * @remarks\n\t * This method keeps the child's allocation stride and does not finalize ID creation ranges.\n\t * Apply creation ranges from {@link IIdCompressorCore.events} separately, in order.\n\t *\n\t * @param token - Synchronization token obtained from this child's parent.\n\t * @throws If the token names another child or contains invalid state.\n\t * @throws `TypeError` if the token's generation count is not a nonnegative safe integer.\n\t */\n\tsynchronizeWithParent(token: ParentShardSynchronizationToken): void;\n\n\t/**\n\t * Returns undefined if this compressor is not part of a shard group, and otherwise returns a synchronization token for this shard\n\t * that can be used when calling {@link IIdCompressorCore.synchronizeWithShard}. This does NOT dispose the shard.\n\t *\n\t * @returns The sync token if this compressor is part of a sharded group, otherwise undefined.\n\t * The returned token is serializable and can be passed across marshaling boundaries. This token can be used to synchronize a\n\t * parent with this compressor by calling {@link IIdCompressorCore.synchronizeWithShard}.\n\t * Unlike {@link IIdCompressorCore.disposeShard}, this is non-destructive and may be called on a shard that still has active\n\t * child shards, which allows an intermediate shard to propagate its progress upward to its own parent.\n\t * @throws If this compressor is the root of the shard tree (only non-root shards can produce a token).\n\t */\n\tgetShardSyncToken(): ShardSynchronizationToken | undefined;\n\n\t/**\n\t * Returns undefined if this compressor is not part of a shard group, and otherwise disposes this shard and returns\n\t * the disposal token for this compressor. If this compressor was part of a shard group, the compressor will no longer be usable.\n\t *\n\t * @returns The disposal token if this compressor is part of a sharded group, otherwise undefined.\n\t * The returned token is serializable and can be passed across marshaling boundaries. Passing it to\n\t * {@link IIdCompressorCore.synchronizeWithShard} on the parent reclaims this shard's subset of the ID space.\n\t * @throws If this shard has active child shards.\n\t * This means that a shard tree must be disposed from the leaves upwards.\n\t */\n\tdisposeShard(): ShardSynchronizationToken | undefined;\n\n\t/**\n\t * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `deserializeIdCompressor()`.\n\t * This includes finalized state as well as un-finalized state and is therefore suitable for use in offline scenarios.\n\t */\n\tserialize(withSession: true): SerializedIdCompressorWithOngoingSession;\n\n\t/**\n\t * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `deserializeIdCompressor()`.\n\t * This only includes finalized state and is therefore suitable for use in summaries.\n\t */\n\tserialize(withSession: false): SerializedIdCompressorWithNoSession;\n}\n\n/**\n * The serialized state of a new child compressor and its initial synchronization token.\n *\n * @sealed\n * @internal\n */\nexport interface SerializedIdCompressorShard {\n\t/**\n\t * Serialized child state that can be rehydrated across a marshalling boundary.\n\t */\n\treadonly serialized: SerializedIdCompressorWithOngoingSession;\n\n\t/**\n\t * Initial, non-disposing token for this child shard.\n\t *\n\t * @remarks\n\t * After the child generates IDs, obtain its updated token before synchronizing\n\t * with the parent or reclaiming the child's ID space.\n\t *\n\t * This can be used to immediately dispose of a new shard if it is not used.\n\t */\n\treadonly syncToken: ShardSynchronizationToken;\n}\n\n/**\n * State shared by shard synchronization tokens.\n * @remarks\n * Treat these tokens as opaque: the compressor owns their interpretation and may add state in the future.\n * The {@link ShardToken.disposed} flag distinguishes ordinary synchronization from shard reclamation.\n * The branded {@link ShardSynchronizationToken} is the concrete child-to-parent token.\n * @internal\n */\nexport interface ShardToken {\n\t/**\n\t * The sending compressor's generation count, which can include backfilled positions\n\t * rather than only IDs it generated.\n\t * @remarks Must be a nonnegative safe integer.\n\t */\n\tlocalGenCount: number;\n\n\t/**\n\t * Identifies the child shard involved in synchronization with its parent.\n\t */\n\tshardId: SessionId;\n\n\t/**\n\t * Whether this token also signals disposal of the originating shard. When `true`, passing the token to\n\t * {@link IIdCompressorCore.synchronizeWithShard} reclaims the shard's ID space in addition to synchronizing.\n\t * Produced as `false` by {@link IIdCompressorCore.getShardSyncToken} and `true` by {@link IIdCompressorCore.disposeShard}.\n\t */\n\tdisposed: boolean;\n}\n\n/**\n * A {@link ShardToken} that identifies a shard for synchronization (and, when {@link ShardToken.disposed} is `true`,\n * reclamation) via {@link IIdCompressorCore.synchronizeWithShard}.\n * @internal\n */\nexport type ShardSynchronizationToken = ShardToken & {\n\treadonly ShardSynchronizationToken: \"c79724e1-9103-4415-95b5-bebb932be404\";\n};\n\n/**\n * A synchronization token from a parent to one of its active child shards.\n * @remarks\n * Unlike the child-to-parent {@link ShardSynchronizationToken}, this token cannot dispose a shard.\n * It does not finalize ID creation ranges.\n * @internal\n */\nexport interface ParentShardSynchronizationToken\n\textends Readonly<Omit<ShardToken, \"disposed\">> {\n\t/** Identifies the parent-to-child token across a message boundary. */\n\treadonly type: \"parentIdSpaceShardSyncToken\";\n}\n\n/**\n * Events for finalized ID creation ranges.\n * @internal\n */\nexport interface IdCompressorEvents {\n\t/** Reports a range after finalization succeeds. */\n\trangeFinalized: (range: IdCreationRange) => void;\n}\n/**\n * A distributed UUID generator and compressor.\n *\n * @remarks\n * `IdCompressor` offers the ability to generate arbitrary non-colliding v4 UUIDs, called stable IDs, while compressing them into small integers\n * for efficient storage and transmission. It also provides the ability to decompress these integers back into their original UUIDs.\n *\n * The compressor is designed to be used in a distributed environment, where multiple clients may be generating IDs concurrently.\n * IDs generated by a compressor, via calls to {@link IIdCompressor.generateCompressedId}, are created in the compressor's \"local\" session.\n * These IDs are unique within the session, but may not be unique across sessions. In the context of Fluid, the scope of a session\n * is the same as the scope of a container. This means that anytime IDs are transferred between sessions, they must be translated\n * through a process called \"normalization\":\n *\n * - The scope of a local session (in which local IDs are unique) is called the \"session space\" of the client.\n * - The context of persisted state (in Fluid, this is the context of ops and summaries) is called \"op space\".\n * - {@link SessionSpaceCompressedId | SessionSpaceCompressedIds}, generated via calls to {@link IIdCompressor.generateCompressedId}, should NEVER be serialized directly.\n * In Fluid, this means they should not be included in ops or summaries.\n * - Before serialization, IDs must be normalized to op space to ensure they are interpretable by other clients.\n * - Upon receipt, IDs should be normalized back to session space before use.\n *\n * @example\n *\n * Client A (Sender)\n * ```typescript\n * // Generate several local IDs\n * const localId1 = idCompressor.generateCompressedId();\n * const localId2 = idCompressor.generateCompressedId();\n * const localId3 = idCompressor.generateCompressedId();\n *\n * // Normalize these IDs to op space for inclusion in a message\n * const opSpaceId1 = idCompressor.normalizeToOpSpace(localId1);\n * const opSpaceId2 = idCompressor.normalizeToOpSpace(localId2);\n * const opSpaceId3 = idCompressor.normalizeToOpSpace(localId3);\n *\n * // Create and send a message containing these op space IDs along with the sender's session ID\n * // In Fluid, this would be an op or summary\n * const message = {\n * sessionID: idCompressor.localSessionId,\n * ids: [opSpaceId1, opSpaceId2, opSpaceId3]\n * };\n * ```\n *\n * Client B (Receiver)\n *\n * ```typescript\n * // Receive the message from Client A\n * const receivedMessage = ...; // In Fluid, this would be an op or summary\n *\n * // Normalize the received IDs back to session space, utilizing the sender's session ID\n * const sessionSpaceId1 = idCompressor.normalizeToSessionSpace(receivedMessage.ids[0], receivedMessage.sessionID);\n * const sessionSpaceId2 = idCompressor.normalizeToSessionSpace(receivedMessage.ids[1], receivedMessage.sessionID);\n * const sessionSpaceId3 = idCompressor.normalizeToSessionSpace(receivedMessage.ids[2], receivedMessage.sessionID);\n * ```\n * @public\n * @sealed\n */\nexport interface IIdCompressor {\n\t/**\n\t * The local session ID.\n\t */\n\tlocalSessionId: SessionId;\n\n\t/**\n\t * Generates a new compressed ID.\n\t *\n\t * @remarks The returned ID is in session space and should not be serialized directly.\n\t * @see {@link IIdCompressor} for more details.\n\t * @returns A new local ID in session space.\n\t */\n\tgenerateCompressedId(): SessionSpaceCompressedId;\n\n\t/**\n\t * Generates a new ID that is guaranteed to be unique across all sessions known to this compressor without the need for any\n\t * normalization.\n\t *\n\t * @remarks The returned ID is not guaranteed to be a compressed ID (small number); it may be a stable ID (UUID string).\n\t * In Fluid, the likelihood of generating the bulkier stable ID is dictated by network conditions and is highly probable in\n\t * scenarios such as offline. This is still useful for use cases where simplicity is more important than performance and\n\t * this approach will often be superior to generating a UUID.\n\t *\n\t * If small numbers are a requirement, {@link IIdCompressor.generateCompressedId} and normalization should be used instead.\n\t * @see {@link IIdCompressor} for more details.\n\t * @returns A new local ID in session space.\n\t */\n\tgenerateDocumentUniqueId(): (SessionSpaceCompressedId & OpSpaceCompressedId) | StableId;\n\n\t/**\n\t * Normalizes a session space ID into op space.\n\t *\n\t * @remarks The returned ID is in op space and can be safely serialized.\n\t * However, it should be {@link IIdCompressor.normalizeToSessionSpace|normalized back to session space} before use.\n\t *\n\t * @param id - The local ID to normalize.\n\t * @returns The ID in op space.\n\t */\n\tnormalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId;\n\n\t/**\n\t * Normalizes an ID into session space.\n\t *\n\t * @remarks\n\t * For this to be valid, `originSessionId` must be the {@link IIdCompressor.localSessionId} of the compressor that normalized `id` to op space.\n\t * Additionally, this compressor must know about the relevant {@link IdCreationRange} from the encoding {@link IIdCompressor}.\n\t * This can be accomplished using creating the compressor using {@link (deserializeIdCompressor:1)},\n\t * or relying on something (like the Fluid runtime) to synchronize the range information using internal mechanisms.\n\t *\n\t * The {@link IIdCompressor} provided by the Fluid Framework runtime will automatically track {@link IdCreationRange | Id Creation Ranges} (via its own ops) from other clients in the container.\n\t * It is up to the user of the {@link IIdCompressor} to ensure the relevant session information is available,\n\t * either by depending on the originating client's session ID for ops, or by explicitly storing it alongside the data containing the {@link OpSpaceCompressedId}.\n\t *\n\t * Note that attachment summaries, while they are ops, are also interpreted as summaries without the context of their originating session,\n\t * so they should explicitly include session IDs if required to decode them just like regular summaries and exported data.\n\t *\n\t * @param id - The ID to normalize.\n\t * @param originSessionId - The {@link IIdCompressor.localSessionId} of the compressor which {@link IIdCompressor.normalizeToOpSpace|encoded} `id`.\n\t * This might not be the client that created `id` in the first place, but rather the client that serialized it.\n\t * This is an important distinction in the case of a reference, where a client might refer to an ID created by another client.\n\t * @returns The session-space ID in the local session corresponding to `id`.\n\t */\n\tnormalizeToSessionSpace(\n\t\tid: OpSpaceCompressedId,\n\t\toriginSessionId: SessionId,\n\t): SessionSpaceCompressedId;\n\n\t/**\n\t * Attempts to normalize an op-space ID into session space without an originator session id.\n\t * This mainly exists for data recovery purposes, when the originator session id was lost.\n\t * When possible, use {@link IIdCompressor.normalizeToSessionSpace} instead.\n\t *\n\t * @remarks\n\t * When the id is a final ID, this will return the properly translated session-space ID,\n\t * or throw if it detects that the final id is not valid in this compressor.\n\t *\n\t * Returns `undefined` if `id` is a non-final op-space ID — those are local to the\n\t * originating session and require its session id to resolve. Callers which have the\n\t * originating session id should call {@link IIdCompressor.normalizeToSessionSpace} instead.\n\t *\n\t * @param id - The ID to normalize.\n\t * @returns The session-space ID corresponding to `id`, or `undefined` if `id` is\n\t * non-final and therefore requires an originator session id to resolve.\n\t * @throws If `id` is final but is not known to this compressor (i.e. it refers to\n\t * a final id that has never been observed as finalized).\n\t * Note that it is possible (even likely) for a finalized id from another id compressor to pass this check,\n\t * and return an incorrect session-space ID.\n\t * This error is thrown only in cases where it is possible to detect the input is invalid,\n\t * and intended to help with catching bugs where invalid identifiers are being provided.\n\t * It cannot be relied upon for validation.\n\t *\n\t * @privateRemarks\n\t * Currently this is only used for data recovery in the case of lost session ids,\n\t * but we could theoretically provide an API to determine if the session id is required when encoding data (some IDs were non-final):\n\t * in such a setup, this API, and/or a version of normalizeToSessionSpace where the session id is optional,\n\t * could be used for a regular non-recovery use-case.\n\t */\n\ttryNormalizeToSessionSpaceWithoutSession(\n\t\tid: OpSpaceCompressedId,\n\t): SessionSpaceCompressedId | undefined;\n\n\t/**\n\t * Decompresses a previously compressed ID into a UUID.\n\t * @param id - The compressed ID to be decompressed.\n\t * @returns The UUID associated with the compressed ID.\n\t * @throws If the ID was not generated by any session known to this compressor.\n\t */\n\tdecompress(id: SessionSpaceCompressedId): StableId;\n\n\t/**\n\t * Recompresses a UUID.\n\t * @param uncompressed - The UUID to recompress.\n\t * @returns The `CompressedId` associated with `uncompressed`.\n\t * @throws If the UUID was not generated by any session known to this compressor.\n\t */\n\trecompress(uncompressed: StableId): SessionSpaceCompressedId;\n\n\t/**\n\t * Attempts to recompress a UUID.\n\t * @param uncompressed - The UUID to recompress.\n\t * @returns The `CompressedId` associated with `uncompressed` or undefined if the UUID was not generated by any session known to this compressor.\n\t */\n\ttryRecompress(uncompressed: StableId): SessionSpaceCompressedId | undefined;\n}\n"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
export type { IdCreationRange, SerializedIdCompressor, SerializedIdCompressorWithNoSession, SerializedIdCompressorWithOngoingSession, } from "./persisted-types/index.js";
|
|
6
|
-
export type { IIdCompressorCore, IIdCompressor } from "./idCompressor.js";
|
|
6
|
+
export type { IIdCompressorCore, IIdCompressor, ShardToken, ShardSynchronizationToken, SerializedIdCompressorShard, ParentShardSynchronizationToken, IdCompressorEvents, } from "./idCompressor.js";
|
|
7
|
+
export { SerializationVersion, type SerializationVersion as SerializationVersionType, } from "./idCompressor.js";
|
|
7
8
|
export type { SessionSpaceCompressedId, OpSpaceCompressedId, SessionId, StableId, } from "./identifiers.js";
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EACX,eAAe,EACf,sBAAsB,EACtB,mCAAmC,EACnC,wCAAwC,GACxC,MAAM,4BAA4B,CAAC;AAEpC,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EACX,eAAe,EACf,sBAAsB,EACtB,mCAAmC,EACnC,wCAAwC,GACxC,MAAM,4BAA4B,CAAC;AAEpC,YAAY,EACX,iBAAiB,EACjB,aAAa,EACb,UAAU,EACV,yBAAyB,EACzB,2BAA2B,EAC3B,+BAA+B,EAC/B,kBAAkB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,oBAAoB,EACpB,KAAK,oBAAoB,IAAI,wBAAwB,GACrD,MAAM,mBAAmB,CAAC;AAE3B,YAAY,EACX,wBAAwB,EACxB,mBAAmB,EACnB,SAAS,EACT,QAAQ,GACR,MAAM,kBAAkB,CAAC"}
|
package/dist/types/index.js
CHANGED
|
@@ -4,4 +4,7 @@
|
|
|
4
4
|
* Licensed under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.SerializationVersion = void 0;
|
|
8
|
+
var idCompressor_js_1 = require("./idCompressor.js");
|
|
9
|
+
Object.defineProperty(exports, "SerializationVersion", { enumerable: true, get: function () { return idCompressor_js_1.SerializationVersion; } });
|
|
7
10
|
//# sourceMappingURL=index.js.map
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport type {\n\tIdCreationRange,\n\tSerializedIdCompressor,\n\tSerializedIdCompressorWithNoSession,\n\tSerializedIdCompressorWithOngoingSession,\n} from \"./persisted-types/index.js\";\n\nexport type {
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAkBH,qDAG2B;AAF1B,uHAAA,oBAAoB,OAAA","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport type {\n\tIdCreationRange,\n\tSerializedIdCompressor,\n\tSerializedIdCompressorWithNoSession,\n\tSerializedIdCompressorWithOngoingSession,\n} from \"./persisted-types/index.js\";\n\nexport type {\n\tIIdCompressorCore,\n\tIIdCompressor,\n\tShardToken,\n\tShardSynchronizationToken,\n\tSerializedIdCompressorShard,\n\tParentShardSynchronizationToken,\n\tIdCompressorEvents,\n} from \"./idCompressor.js\";\nexport {\n\tSerializationVersion,\n\ttype SerializationVersion as SerializationVersionType,\n} from \"./idCompressor.js\";\n\nexport type {\n\tSessionSpaceCompressedId,\n\tOpSpaceCompressedId,\n\tSessionId,\n\tStableId,\n} from \"./identifiers.js\";\n"]}
|
|
@@ -5,21 +5,21 @@
|
|
|
5
5
|
import type { SessionId } from "../identifiers.js";
|
|
6
6
|
/**
|
|
7
7
|
* The serialized contents of an IdCompressor, suitable for persistence in a summary.
|
|
8
|
-
* @
|
|
8
|
+
* @internal
|
|
9
9
|
*/
|
|
10
10
|
export type SerializedIdCompressor = string & {
|
|
11
11
|
readonly _serializedIdCompressor: "8c73c57c-1cf4-4278-8915-6444cb4f6af5";
|
|
12
12
|
};
|
|
13
13
|
/**
|
|
14
14
|
* The serialized contents of an IdCompressor, suitable for persistence in a summary.
|
|
15
|
-
* @
|
|
15
|
+
* @internal
|
|
16
16
|
*/
|
|
17
17
|
export type SerializedIdCompressorWithNoSession = SerializedIdCompressor & {
|
|
18
18
|
readonly _noLocalState: "3aa2e1e8-cc28-4ea7-bc1a-a11dc3f26dfb";
|
|
19
19
|
};
|
|
20
20
|
/**
|
|
21
21
|
* The serialized contents of an IdCompressor, suitable for persistence in a summary.
|
|
22
|
-
* @
|
|
22
|
+
* @internal
|
|
23
23
|
*/
|
|
24
24
|
export type SerializedIdCompressorWithOngoingSession = SerializedIdCompressor & {
|
|
25
25
|
readonly _hasLocalState: "1281acae-6d14-47e7-bc92-71c8ee0819cb";
|
|
@@ -28,7 +28,7 @@ export type SerializedIdCompressorWithOngoingSession = SerializedIdCompressor &
|
|
|
28
28
|
* Data describing a range of session-local IDs (from a remote or local session).
|
|
29
29
|
*
|
|
30
30
|
* A range is composed of local IDs that were generated.
|
|
31
|
-
* @
|
|
31
|
+
* @internal
|
|
32
32
|
*/
|
|
33
33
|
export interface IdCreationRange {
|
|
34
34
|
readonly sessionId: SessionId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"0.0.1.js","sourceRoot":"","sources":["../../../src/types/persisted-types/0.0.1.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type { SessionId } from \"../identifiers.js\";\n\n/**\n * The serialized contents of an IdCompressor, suitable for persistence in a summary.\n * @
|
|
1
|
+
{"version":3,"file":"0.0.1.js","sourceRoot":"","sources":["../../../src/types/persisted-types/0.0.1.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type { SessionId } from \"../identifiers.js\";\n\n/**\n * The serialized contents of an IdCompressor, suitable for persistence in a summary.\n * @internal\n */\nexport type SerializedIdCompressor = string & {\n\treadonly _serializedIdCompressor: \"8c73c57c-1cf4-4278-8915-6444cb4f6af5\";\n};\n\n/**\n * The serialized contents of an IdCompressor, suitable for persistence in a summary.\n * @internal\n */\nexport type SerializedIdCompressorWithNoSession = SerializedIdCompressor & {\n\treadonly _noLocalState: \"3aa2e1e8-cc28-4ea7-bc1a-a11dc3f26dfb\";\n};\n\n/**\n * The serialized contents of an IdCompressor, suitable for persistence in a summary.\n * @internal\n */\nexport type SerializedIdCompressorWithOngoingSession = SerializedIdCompressor & {\n\treadonly _hasLocalState: \"1281acae-6d14-47e7-bc92-71c8ee0819cb\";\n};\n\n/**\n * Data describing a range of session-local IDs (from a remote or local session).\n *\n * A range is composed of local IDs that were generated.\n * @internal\n */\nexport interface IdCreationRange {\n\treadonly sessionId: SessionId;\n\treadonly ids?: {\n\t\t/**\n\t\t * The gen count of the first ID in the range created by `sessionId.`\n\t\t */\n\t\treadonly firstGenCount: number;\n\n\t\t/**\n\t\t * The number of IDs created in the range created by `sessionId.`\n\t\t */\n\t\treadonly count: number;\n\n\t\t/**\n\t\t * The size of the ID cluster to create if `count` overflows the existing cluster for\n\t\t * `sessionId`, if one exists. This request will be respected, and the size of the cluster\n\t\t * will be equal to overflow + `requestedClusterSize`.\n\t\t */\n\t\treadonly requestedClusterSize: number;\n\n\t\t/**\n\t\t * The ranges of IDs that were allocated as local IDs in the range created by `sessionId.`\n\t\t */\n\t\treadonly localIdRanges: [genCount: number, count: number][];\n\t};\n}\n"]}
|
package/dist/utilities.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { LocalCompressedId, NumericUuid } from "./identifiers.js";
|
|
|
6
6
|
import type { SessionId, StableId } from "./types/index.js";
|
|
7
7
|
/**
|
|
8
8
|
* Generate a random session ID
|
|
9
|
-
* @
|
|
9
|
+
* @internal
|
|
10
10
|
*/
|
|
11
11
|
export declare function createSessionId(): SessionId;
|
|
12
12
|
/**
|