@fluidframework/id-compressor 3.3.0 → 3.5.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/idCompressor.d.ts +10 -17
  3. package/dist/idCompressor.d.ts.map +1 -1
  4. package/dist/idCompressor.js +68 -35
  5. package/dist/idCompressor.js.map +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/packageVersion.d.ts +1 -1
  10. package/dist/packageVersion.js +1 -1
  11. package/dist/packageVersion.js.map +1 -1
  12. package/dist/test/sharding.fuzz.spec.js.map +1 -1
  13. package/dist/test/sharding.spec.js.map +1 -1
  14. package/dist/test/types/validateIdCompressorPrevious.generated.js.map +1 -1
  15. package/dist/types/idCompressor.d.ts +110 -31
  16. package/dist/types/idCompressor.d.ts.map +1 -1
  17. package/dist/types/idCompressor.js.map +1 -1
  18. package/dist/types/index.d.ts +1 -1
  19. package/dist/types/index.d.ts.map +1 -1
  20. package/dist/types/index.js.map +1 -1
  21. package/lib/idCompressor.d.ts +10 -17
  22. package/lib/idCompressor.d.ts.map +1 -1
  23. package/lib/idCompressor.js +69 -36
  24. package/lib/idCompressor.js.map +1 -1
  25. package/lib/index.d.ts +1 -1
  26. package/lib/index.d.ts.map +1 -1
  27. package/lib/index.js.map +1 -1
  28. package/lib/packageVersion.d.ts +1 -1
  29. package/lib/packageVersion.js +1 -1
  30. package/lib/packageVersion.js.map +1 -1
  31. package/lib/test/sharding.fuzz.spec.js.map +1 -1
  32. package/lib/test/sharding.spec.js.map +1 -1
  33. package/lib/test/types/validateIdCompressorPrevious.generated.js.map +1 -1
  34. package/lib/types/idCompressor.d.ts +110 -31
  35. package/lib/types/idCompressor.d.ts.map +1 -1
  36. package/lib/types/idCompressor.js.map +1 -1
  37. package/lib/types/index.d.ts +1 -1
  38. package/lib/types/index.d.ts.map +1 -1
  39. package/lib/types/index.js.map +1 -1
  40. package/package.json +9 -42
  41. package/src/idCompressor.ts +90 -42
  42. package/src/index.ts +10 -7
  43. package/src/packageVersion.ts +1 -1
  44. package/src/test/types/validateIdCompressorPrevious.generated.ts +1 -121
  45. package/src/types/idCompressor.ts +119 -31
  46. package/src/types/index.ts +3 -0
@@ -3,6 +3,7 @@
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";
7
8
  /**
8
9
  * Serialization format versions for IdCompressor.
@@ -87,6 +88,10 @@ export type SerializationVersion = (typeof SerializationVersion)[keyof typeof Se
87
88
  * @internal
88
89
  */
89
90
  export interface IIdCompressorCore {
91
+ /**
92
+ * Events emitted by the ID compressor.
93
+ */
94
+ readonly events: Listenable<IdCompressorEvents>;
90
95
  /**
91
96
  * Returns a range of IDs created by this session in a format for sending to the server for finalizing.
92
97
  * The range will include all IDs generated via calls to `generateCompressedId` since the last time a
@@ -155,12 +160,11 @@ export interface IIdCompressorCore {
155
160
  * @param newShardCount - The number of additional different shards to split this compressor into.
156
161
  * Must be a positive safe integer.
157
162
  * @throws If `newShardCount` is not a positive safe integer or the resulting stride exceeds the sharding limit.
158
- * @returns An array of serialized compressors of size `newShardCount`.
159
- * These can be passed across a marshalling boundary and rehydrated on the other side, and will safely share the ID space of `this`.
163
+ * @returns An array of `newShardCount` {@link SerializedIdCompressorShard} objects.
160
164
  * Note that this method should only be needed when multiple JS runtimes are in play, as sharded compressors essentially
161
165
  * attempt to emulate a single static compressor and any code running in the same JS runtime can simply use statics.
162
166
  */
163
- shard(newShardCount: number): SerializedIdCompressorWithOngoingSession[];
167
+ shard(newShardCount: number): SerializedIdCompressorShard[];
164
168
  /**
165
169
  * Synchronizes `this` compressor with a child shard. Synchronization will occur for the state of the child at the time `syncToken`
166
170
  * was generated, meaning that `this` compressor can use/ingest IDs generated up to that point. Attempts to use IDs from a child shard
@@ -172,8 +176,32 @@ export interface IIdCompressorCore {
172
176
  * be disposed from the leaves upwards.
173
177
  * @param syncToken - The token for the shard, obtained by calling {@link IIdCompressorCore.getShardSyncToken} (non-destructive
174
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.
175
180
  */
176
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;
177
205
  /**
178
206
  * Returns undefined if this compressor is not part of a shard group, and otherwise returns a synchronization token for this shard
179
207
  * that can be used when calling {@link IIdCompressorCore.synchronizeWithShard}. This does NOT dispose the shard.
@@ -209,23 +237,44 @@ export interface IIdCompressorCore {
209
237
  serialize(withSession: false): SerializedIdCompressorWithNoSession;
210
238
  }
211
239
  /**
212
- * The state shared by all shard tokens: enough information to identify a shard and its progress
213
- * through its stride pattern. This is used to track which shard generated which IDs and to manage
214
- * reclamation of a disposed shard's ID space. The {@link ShardToken.disposed} flag distinguishes a
215
- * plain synchronization token from a disposal token. The branded {@link ShardSynchronizationToken}
216
- * is the concrete type handed to consumers.
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.
217
267
  * @internal
218
268
  */
219
269
  export interface ShardToken {
220
270
  /**
221
- * The number of positions filled in this shard's stride pattern.
222
- * This tracks progress through the stride cycle, not the count of IDs actually generated.
223
- * For example, when a shard is created, it backfills entries for positions in its stride,
224
- * so this value may be non-zero even if the shard hasn't generated any IDs yet.
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.
225
274
  */
226
275
  localGenCount: number;
227
276
  /**
228
- * Unique identifier for this shard within its parent.
277
+ * Identifies the child shard involved in synchronization with its parent.
229
278
  */
230
279
  shardId: SessionId;
231
280
  /**
@@ -243,27 +292,48 @@ export interface ShardToken {
243
292
  export type ShardSynchronizationToken = ShardToken & {
244
293
  readonly ShardSynchronizationToken: "c79724e1-9103-4415-95b5-bebb932be404";
245
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
+ }
246
314
  /**
247
315
  * A distributed UUID generator and compressor.
316
+ *
317
+ * @remarks
248
318
  * `IdCompressor` offers the ability to generate arbitrary non-colliding v4 UUIDs, called stable IDs, while compressing them into small integers
249
319
  * for efficient storage and transmission. It also provides the ability to decompress these integers back into their original UUIDs.
250
320
  *
251
321
  * The compressor is designed to be used in a distributed environment, where multiple clients may be generating IDs concurrently.
252
- * IDs generated by a compressor, via calls to `generateCompressedId`, are created in the compressor's "local" session.
322
+ * IDs generated by a compressor, via calls to {@link IIdCompressor.generateCompressedId}, are created in the compressor's "local" session.
253
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
254
324
  * is the same as the scope of a container. This means that anytime IDs are transferred between sessions, they must be translated
255
325
  * through a process called "normalization":
326
+ *
256
327
  * - The scope of a local session (in which local IDs are unique) is called the "session space" of the client.
257
328
  * - The context of persisted state (in Fluid, this is the context of ops and summaries) is called "op space".
258
- * - `SessionSpaceCompressedId`s, generated via calls to `generateCompressedId`, should NEVER be serialized directly.
329
+ * - {@link SessionSpaceCompressedId | SessionSpaceCompressedIds}, generated via calls to {@link IIdCompressor.generateCompressedId}, should NEVER be serialized directly.
259
330
  * In Fluid, this means they should not be included in ops or summaries.
260
331
  * - Before serialization, IDs must be normalized to op space to ensure they are interpretable by other clients.
261
332
  * - Upon receipt, IDs should be normalized back to session space before use.
262
333
  *
263
- * Example Usage:
264
- *
265
- * ### Client A (Sender)
334
+ * @example
266
335
  *
336
+ * Client A (Sender)
267
337
  * ```typescript
268
338
  * // Generate several local IDs
269
339
  * const localId1 = idCompressor.generateCompressedId();
@@ -283,7 +353,7 @@ export type ShardSynchronizationToken = ShardToken & {
283
353
  * };
284
354
  * ```
285
355
  *
286
- * ### Client B (Receiver)
356
+ * Client B (Receiver)
287
357
  *
288
358
  * ```typescript
289
359
  * // Receive the message from Client A
@@ -304,48 +374,57 @@ export interface IIdCompressor {
304
374
  localSessionId: SessionId;
305
375
  /**
306
376
  * Generates a new compressed ID.
307
- * The returned ID is in session space and should not be serialized directly. See `IIdCompressor` for more details.
377
+ *
378
+ * @remarks The returned ID is in session space and should not be serialized directly.
379
+ * @see {@link IIdCompressor} for more details.
308
380
  * @returns A new local ID in session space.
309
381
  */
310
382
  generateCompressedId(): SessionSpaceCompressedId;
311
383
  /**
312
384
  * Generates a new ID that is guaranteed to be unique across all sessions known to this compressor without the need for any
313
- * normalization. The returned ID is not guaranteed to be a compressed ID (small number); it may be a stable ID (UUID string).
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).
314
388
  * In Fluid, the likelihood of generating the bulkier stable ID is dictated by network conditions and is highly probable in
315
389
  * scenarios such as offline. This is still useful for use cases where simplicity is more important than performance and
316
390
  * this approach will often be superior to generating a UUID.
317
- * If small numbers are a requirement, `generateCompressedId` and normalization should be used instead.
318
- * See `IIdCompressor` for more details.
391
+ *
392
+ * If small numbers are a requirement, {@link IIdCompressor.generateCompressedId} and normalization should be used instead.
393
+ * @see {@link IIdCompressor} for more details.
319
394
  * @returns A new local ID in session space.
320
395
  */
321
396
  generateDocumentUniqueId(): (SessionSpaceCompressedId & OpSpaceCompressedId) | StableId;
322
397
  /**
323
398
  * Normalizes a session space ID into op space.
324
- * The returned ID is in op space and can be safely serialized.
399
+ *
400
+ * @remarks The returned ID is in op space and can be safely serialized.
325
401
  * However, it should be {@link IIdCompressor.normalizeToSessionSpace|normalized back to session space} before use.
402
+ *
326
403
  * @param id - The local ID to normalize.
327
404
  * @returns The ID in op space.
328
405
  */
329
406
  normalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId;
330
407
  /**
331
408
  * Normalizes an ID into session space.
332
- * @param id - The ID to normalize.
333
- * @param originSessionId - The {@link IIdCompressor.localSessionId} of the compressor which {@link IIdCompressor.normalizeToOpSpace|encoded} `id`.
334
- * This might not be the client that created `id` in the first place, but rather the client that serialized it.
335
- * This is an important distinction in the case of a reference, where a client might refer to an ID created by another client.
336
- * @returns The session-space ID in the local session corresponding to `id`.
409
+ *
337
410
  * @remarks
338
411
  * For this to be valid, `originSessionId` must be the {@link IIdCompressor.localSessionId} of the compressor that normalized `id` to op space.
339
- * Additionally, this compressor must know about the relevant {@link IdCreationRange} from the encoding `IIdCompressor`.
412
+ * Additionally, this compressor must know about the relevant {@link IdCreationRange} from the encoding {@link IIdCompressor}.
340
413
  * This can be accomplished using creating the compressor using {@link (deserializeIdCompressor:1)},
341
414
  * or relying on something (like the Fluid runtime) to synchronize the range information using internal mechanisms.
342
415
  *
343
- * 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.
344
417
  * It is up to the user of the {@link IIdCompressor} to ensure the relevant session information is available,
345
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}.
346
419
  *
347
420
  * Note that attachment summaries, while they are ops, are also interpreted as summaries without the context of their originating session,
348
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`.
349
428
  */
350
429
  normalizeToSessionSpace(id: OpSpaceCompressedId, originSessionId: SessionId): SessionSpaceCompressedId;
351
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;;;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;;;;;;;;;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;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,aAAa,EAAE,MAAM,GAAG,wCAAwC,EAAE,CAAC;IAEzE;;;;;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;;GAGG;AACH,MAAM,CAAC,MAAM,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 {\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 * 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 serialized compressors of size `newShardCount`.\n\t * These can be passed across a marshalling boundary and rehydrated on the other side, and will safely share the ID space of `this`.\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): SerializedIdCompressorWithOngoingSession[];\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 */\n\tsynchronizeWithShard(syncToken: ShardSynchronizationToken): 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 state shared by all shard tokens: enough information to identify a shard and its progress\n * through its stride pattern. This is used to track which shard generated which IDs and to manage\n * reclamation of a disposed shard's ID space. The {@link ShardToken.disposed} flag distinguishes a\n * plain synchronization token from a disposal token. The branded {@link ShardSynchronizationToken}\n * is the concrete type handed to consumers.\n * @internal\n */\nexport interface ShardToken {\n\t/**\n\t * The number of positions filled in this shard's stride pattern.\n\t * This tracks progress through the stride cycle, not the count of IDs actually generated.\n\t * For example, when a shard is created, it backfills entries for positions in its stride,\n\t * so this value may be non-zero even if the shard hasn't generated any IDs yet.\n\t */\n\tlocalGenCount: number;\n\n\t/**\n\t * Unique identifier for this shard within 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 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;AACH,MAAM,CAAC,MAAM,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"]}
@@ -3,7 +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, ShardToken, ShardSynchronizationToken, } from "./idCompressor.js";
6
+ export type { IIdCompressorCore, IIdCompressor, ShardToken, ShardSynchronizationToken, SerializedIdCompressorShard, ParentShardSynchronizationToken, IdCompressorEvents, } from "./idCompressor.js";
7
7
  export { SerializationVersion, type SerializationVersion as SerializationVersionType, } from "./idCompressor.js";
8
8
  export type { SessionSpaceCompressedId, OpSpaceCompressedId, SessionId, StableId, } from "./identifiers.js";
9
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,EACX,iBAAiB,EACjB,aAAa,EACb,UAAU,EACV,yBAAyB,GACzB,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"}
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"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,OAAO,EACN,oBAAoB,GAEpB,MAAM,mBAAmB,CAAC","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} 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"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkBH,OAAO,EACN,oBAAoB,GAEpB,MAAM,mBAAmB,CAAC","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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/id-compressor",
3
- "version": "3.3.0",
3
+ "version": "3.5.0",
4
4
  "description": "ID compressor",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -61,24 +61,24 @@
61
61
  "temp-directory": "nyc/.nyc_output"
62
62
  },
63
63
  "dependencies": {
64
- "@fluid-internal/client-utils": "~3.3.0",
65
- "@fluidframework/core-interfaces": "~3.3.0",
66
- "@fluidframework/core-utils": "~3.3.0",
67
- "@fluidframework/telemetry-utils": "~3.3.0",
64
+ "@fluid-internal/client-utils": "~3.5.0",
65
+ "@fluidframework/core-interfaces": "~3.5.0",
66
+ "@fluidframework/core-utils": "~3.5.0",
67
+ "@fluidframework/telemetry-utils": "~3.5.0",
68
68
  "@tylerbu/sorted-btree-es6": "^2.1.1",
69
69
  "uuid": "^11.1.0"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@arethetypeswrong/cli": "^0.18.5",
73
73
  "@biomejs/biome": "~2.4.5",
74
- "@fluid-internal/mocha-test-setup": "~3.3.0",
75
- "@fluid-private/stochastic-test-utils": "~3.3.0",
74
+ "@fluid-internal/mocha-test-setup": "~3.5.0",
75
+ "@fluid-private/stochastic-test-utils": "~3.5.0",
76
76
  "@fluid-tools/benchmark": "^0.59.0",
77
77
  "@fluid-tools/build-cli": "^0.67.0",
78
78
  "@fluidframework/build-common": "^2.0.3",
79
79
  "@fluidframework/build-tools": "^0.67.0",
80
80
  "@fluidframework/eslint-config-fluid": "^14.0.0",
81
- "@fluidframework/id-compressor-previous": "npm:@fluidframework/id-compressor@3.2.0",
81
+ "@fluidframework/id-compressor-previous": "npm:@fluidframework/id-compressor@3.4.0",
82
82
  "@microsoft/api-extractor": "7.58.1",
83
83
  "@types/mocha": "^10.0.10",
84
84
  "@types/node": "~22.19.17",
@@ -94,40 +94,7 @@
94
94
  "typescript": "~6.0.3"
95
95
  },
96
96
  "typeValidation": {
97
- "broken": {
98
- "Function_createIdCompressor": {
99
- "backCompat": false,
100
- "forwardCompat": false
101
- },
102
- "Function_deserializeIdCompressor": {
103
- "backCompat": false,
104
- "forwardCompat": false
105
- },
106
- "Function_createSessionId": {
107
- "backCompat": false,
108
- "forwardCompat": false
109
- },
110
- "Function_serializeIdCompressor": {
111
- "backCompat": false,
112
- "forwardCompat": false
113
- },
114
- "Interface_IdCreationRange": {
115
- "backCompat": false,
116
- "forwardCompat": false
117
- },
118
- "TypeAlias_SerializedIdCompressor": {
119
- "backCompat": false,
120
- "forwardCompat": false
121
- },
122
- "TypeAlias_SerializedIdCompressorWithNoSession": {
123
- "backCompat": false,
124
- "forwardCompat": false
125
- },
126
- "TypeAlias_SerializedIdCompressorWithOngoingSession": {
127
- "backCompat": false,
128
- "forwardCompat": false
129
- }
130
- },
97
+ "broken": {},
131
98
  "entrypoint": "legacy"
132
99
  },
133
100
  "scripts": {