@fluidframework/container-runtime 2.0.0-dev.4.2.0.153917 → 2.0.0-dev.4.3.0.157531
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/dist/containerRuntime.d.ts +33 -3
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +187 -58
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStoreContext.d.ts +2 -1
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +3 -0
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStores.d.ts +5 -5
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +3 -6
- package/dist/dataStores.js.map +1 -1
- package/dist/gc/garbageCollection.d.ts.map +1 -1
- package/dist/gc/garbageCollection.js +5 -5
- package/dist/gc/garbageCollection.js.map +1 -1
- package/dist/gc/gcConfigs.d.ts.map +1 -1
- package/dist/gc/gcConfigs.js +1 -3
- package/dist/gc/gcConfigs.js.map +1 -1
- package/dist/gc/gcDefinitions.js +1 -1
- package/dist/gc/gcDefinitions.js.map +1 -1
- package/dist/id-compressor/appendOnlySortedMap.d.ts +146 -0
- package/dist/id-compressor/appendOnlySortedMap.d.ts.map +1 -0
- package/dist/id-compressor/appendOnlySortedMap.js +360 -0
- package/dist/id-compressor/appendOnlySortedMap.js.map +1 -0
- package/dist/id-compressor/idCompressor.d.ts +279 -0
- package/dist/id-compressor/idCompressor.d.ts.map +1 -0
- package/dist/id-compressor/idCompressor.js +1258 -0
- package/dist/id-compressor/idCompressor.js.map +1 -0
- package/dist/id-compressor/idRange.d.ts +11 -0
- package/dist/id-compressor/idRange.d.ts.map +1 -0
- package/dist/id-compressor/idRange.js +29 -0
- package/dist/id-compressor/idRange.js.map +1 -0
- package/dist/id-compressor/index.d.ts +14 -0
- package/dist/id-compressor/index.d.ts.map +1 -0
- package/dist/id-compressor/index.js +38 -0
- package/dist/id-compressor/index.js.map +1 -0
- package/dist/id-compressor/numericUuid.d.ts +59 -0
- package/dist/id-compressor/numericUuid.d.ts.map +1 -0
- package/dist/id-compressor/numericUuid.js +325 -0
- package/dist/id-compressor/numericUuid.js.map +1 -0
- package/dist/id-compressor/sessionIdNormalizer.d.ts +138 -0
- package/dist/id-compressor/sessionIdNormalizer.d.ts.map +1 -0
- package/dist/id-compressor/sessionIdNormalizer.js +488 -0
- package/dist/id-compressor/sessionIdNormalizer.js.map +1 -0
- package/dist/id-compressor/utils.d.ts +57 -0
- package/dist/id-compressor/utils.d.ts.map +1 -0
- package/dist/id-compressor/utils.js +90 -0
- package/dist/id-compressor/utils.js.map +1 -0
- package/dist/id-compressor/uuidUtilities.d.ts +30 -0
- package/dist/id-compressor/uuidUtilities.d.ts.map +1 -0
- package/dist/id-compressor/uuidUtilities.js +106 -0
- package/dist/id-compressor/uuidUtilities.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/opLifecycle/batchManager.d.ts +9 -2
- package/dist/opLifecycle/batchManager.d.ts.map +1 -1
- package/dist/opLifecycle/batchManager.js +21 -2
- package/dist/opLifecycle/batchManager.js.map +1 -1
- package/dist/opLifecycle/index.d.ts +1 -1
- package/dist/opLifecycle/index.d.ts.map +1 -1
- package/dist/opLifecycle/index.js.map +1 -1
- package/dist/opLifecycle/opGroupingManager.d.ts.map +1 -1
- package/dist/opLifecycle/opGroupingManager.js +5 -0
- package/dist/opLifecycle/opGroupingManager.js.map +1 -1
- package/dist/opLifecycle/outbox.d.ts +2 -2
- package/dist/opLifecycle/outbox.d.ts.map +1 -1
- package/dist/opLifecycle/outbox.js +34 -22
- package/dist/opLifecycle/outbox.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/pendingStateManager.d.ts +1 -1
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +11 -3
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/summary/index.d.ts +1 -1
- package/dist/summary/index.d.ts.map +1 -1
- package/dist/summary/index.js +2 -1
- package/dist/summary/index.js.map +1 -1
- package/dist/summary/orderedClientElection.d.ts +1 -0
- package/dist/summary/orderedClientElection.d.ts.map +1 -1
- package/dist/summary/orderedClientElection.js +19 -0
- package/dist/summary/orderedClientElection.js.map +1 -1
- package/dist/summary/runningSummarizer.d.ts +0 -1
- package/dist/summary/runningSummarizer.d.ts.map +1 -1
- package/dist/summary/runningSummarizer.js +1 -17
- package/dist/summary/runningSummarizer.js.map +1 -1
- package/dist/summary/summaryFormat.d.ts +3 -0
- package/dist/summary/summaryFormat.d.ts.map +1 -1
- package/dist/summary/summaryFormat.js +3 -1
- package/dist/summary/summaryFormat.js.map +1 -1
- package/dist/summary/summaryManager.d.ts.map +1 -1
- package/dist/summary/summaryManager.js +2 -0
- package/dist/summary/summaryManager.js.map +1 -1
- package/lib/containerRuntime.d.ts +33 -3
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +170 -60
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStoreContext.d.ts +2 -1
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +3 -0
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStores.d.ts +5 -5
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +3 -6
- package/lib/dataStores.js.map +1 -1
- package/lib/gc/garbageCollection.d.ts.map +1 -1
- package/lib/gc/garbageCollection.js +5 -5
- package/lib/gc/garbageCollection.js.map +1 -1
- package/lib/gc/gcConfigs.d.ts.map +1 -1
- package/lib/gc/gcConfigs.js +1 -3
- package/lib/gc/gcConfigs.js.map +1 -1
- package/lib/gc/gcDefinitions.js +1 -1
- package/lib/gc/gcDefinitions.js.map +1 -1
- package/lib/id-compressor/appendOnlySortedMap.d.ts +146 -0
- package/lib/id-compressor/appendOnlySortedMap.d.ts.map +1 -0
- package/lib/id-compressor/appendOnlySortedMap.js +355 -0
- package/lib/id-compressor/appendOnlySortedMap.js.map +1 -0
- package/lib/id-compressor/idCompressor.d.ts +279 -0
- package/lib/id-compressor/idCompressor.d.ts.map +1 -0
- package/lib/id-compressor/idCompressor.js +1248 -0
- package/lib/id-compressor/idCompressor.js.map +1 -0
- package/lib/id-compressor/idRange.d.ts +11 -0
- package/lib/id-compressor/idRange.d.ts.map +1 -0
- package/lib/id-compressor/idRange.js +25 -0
- package/lib/id-compressor/idRange.js.map +1 -0
- package/lib/id-compressor/index.d.ts +14 -0
- package/lib/id-compressor/index.d.ts.map +1 -0
- package/lib/id-compressor/index.js +14 -0
- package/lib/id-compressor/index.js.map +1 -0
- package/lib/id-compressor/numericUuid.d.ts +59 -0
- package/lib/id-compressor/numericUuid.d.ts.map +1 -0
- package/lib/id-compressor/numericUuid.js +315 -0
- package/lib/id-compressor/numericUuid.js.map +1 -0
- package/lib/id-compressor/sessionIdNormalizer.d.ts +138 -0
- package/lib/id-compressor/sessionIdNormalizer.d.ts.map +1 -0
- package/lib/id-compressor/sessionIdNormalizer.js +484 -0
- package/lib/id-compressor/sessionIdNormalizer.js.map +1 -0
- package/lib/id-compressor/utils.d.ts +57 -0
- package/lib/id-compressor/utils.d.ts.map +1 -0
- package/lib/id-compressor/utils.js +79 -0
- package/lib/id-compressor/utils.js.map +1 -0
- package/lib/id-compressor/uuidUtilities.d.ts +30 -0
- package/lib/id-compressor/uuidUtilities.d.ts.map +1 -0
- package/lib/id-compressor/uuidUtilities.js +98 -0
- package/lib/id-compressor/uuidUtilities.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/opLifecycle/batchManager.d.ts +9 -2
- package/lib/opLifecycle/batchManager.d.ts.map +1 -1
- package/lib/opLifecycle/batchManager.js +19 -1
- package/lib/opLifecycle/batchManager.js.map +1 -1
- package/lib/opLifecycle/index.d.ts +1 -1
- package/lib/opLifecycle/index.d.ts.map +1 -1
- package/lib/opLifecycle/index.js.map +1 -1
- package/lib/opLifecycle/opGroupingManager.d.ts.map +1 -1
- package/lib/opLifecycle/opGroupingManager.js +5 -0
- package/lib/opLifecycle/opGroupingManager.js.map +1 -1
- package/lib/opLifecycle/outbox.d.ts +2 -2
- package/lib/opLifecycle/outbox.d.ts.map +1 -1
- package/lib/opLifecycle/outbox.js +35 -23
- package/lib/opLifecycle/outbox.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/pendingStateManager.d.ts +1 -1
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +11 -3
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/summary/index.d.ts +1 -1
- package/lib/summary/index.d.ts.map +1 -1
- package/lib/summary/index.js +1 -1
- package/lib/summary/index.js.map +1 -1
- package/lib/summary/orderedClientElection.d.ts +1 -0
- package/lib/summary/orderedClientElection.d.ts.map +1 -1
- package/lib/summary/orderedClientElection.js +19 -0
- package/lib/summary/orderedClientElection.js.map +1 -1
- package/lib/summary/runningSummarizer.d.ts +0 -1
- package/lib/summary/runningSummarizer.d.ts.map +1 -1
- package/lib/summary/runningSummarizer.js +1 -17
- package/lib/summary/runningSummarizer.js.map +1 -1
- package/lib/summary/summaryFormat.d.ts +3 -0
- package/lib/summary/summaryFormat.d.ts.map +1 -1
- package/lib/summary/summaryFormat.js +2 -0
- package/lib/summary/summaryFormat.js.map +1 -1
- package/lib/summary/summaryManager.d.ts.map +1 -1
- package/lib/summary/summaryManager.js +2 -0
- package/lib/summary/summaryManager.js.map +1 -1
- package/package.json +27 -18
- package/src/containerRuntime.ts +256 -88
- package/src/dataStoreContext.ts +6 -0
- package/src/dataStores.ts +4 -7
- package/src/gc/garbageCollection.ts +7 -6
- package/src/gc/gcConfigs.ts +1 -3
- package/src/gc/gcDefinitions.ts +1 -1
- package/src/id-compressor/README.md +3 -0
- package/src/id-compressor/appendOnlySortedMap.ts +427 -0
- package/src/id-compressor/idCompressor.ts +1854 -0
- package/src/id-compressor/idRange.ts +35 -0
- package/src/id-compressor/index.ts +35 -0
- package/src/id-compressor/numericUuid.ts +383 -0
- package/src/id-compressor/sessionIdNormalizer.ts +609 -0
- package/src/id-compressor/utils.ts +114 -0
- package/src/id-compressor/uuidUtilities.ts +123 -0
- package/src/index.ts +1 -0
- package/src/opLifecycle/README.md +13 -0
- package/src/opLifecycle/batchManager.ts +35 -2
- package/src/opLifecycle/index.ts +1 -1
- package/src/opLifecycle/opGroupingManager.ts +5 -1
- package/src/opLifecycle/outbox.ts +57 -23
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +21 -7
- package/src/summary/index.ts +1 -0
- package/src/summary/orderedClientElection.ts +17 -1
- package/src/summary/runningSummarizer.ts +3 -24
- package/src/summary/summaryFormat.ts +4 -0
- package/src/summary/summaryManager.ts +2 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { assert } from "@fluidframework/common-utils";
|
|
7
|
+
import type { IdCreationRange, UnackedLocalId } from "@fluidframework/runtime-definitions";
|
|
8
|
+
|
|
9
|
+
export function getIds(
|
|
10
|
+
range: IdCreationRange,
|
|
11
|
+
):
|
|
12
|
+
| { first: UnackedLocalId; last: UnackedLocalId; overrides?: IdCreationRange.Overrides }
|
|
13
|
+
| undefined {
|
|
14
|
+
const { ids } = range;
|
|
15
|
+
if (ids === undefined) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let first = ids.first;
|
|
20
|
+
let last = ids.last;
|
|
21
|
+
|
|
22
|
+
const overrides = ids as Partial<IdCreationRange.HasOverrides>;
|
|
23
|
+
if (overrides.overrides !== undefined) {
|
|
24
|
+
first ??= overrides.overrides[0][0];
|
|
25
|
+
last ??= overrides.overrides[overrides.overrides.length - 1][0];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
assert(first !== undefined && last !== undefined, 0x49b /* malformed IdCreationRange */);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
first,
|
|
32
|
+
last,
|
|
33
|
+
overrides: overrides.overrides,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Exports for `id-compressor`
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
defaultClusterCapacity,
|
|
12
|
+
hasOngoingSession,
|
|
13
|
+
IdCompressor,
|
|
14
|
+
isFinalId,
|
|
15
|
+
isLocalId,
|
|
16
|
+
} from "./idCompressor";
|
|
17
|
+
export { getIds } from "./idRange";
|
|
18
|
+
export {
|
|
19
|
+
NumericUuid,
|
|
20
|
+
numericUuidEquals,
|
|
21
|
+
getPositiveDelta,
|
|
22
|
+
createSessionId,
|
|
23
|
+
numericUuidFromStableId,
|
|
24
|
+
stableIdFromNumericUuid,
|
|
25
|
+
incrementUuid,
|
|
26
|
+
ensureSessionUuid,
|
|
27
|
+
} from "./numericUuid";
|
|
28
|
+
export {
|
|
29
|
+
generateStableId,
|
|
30
|
+
assertIsStableId,
|
|
31
|
+
isStableId,
|
|
32
|
+
assertIsUuidString,
|
|
33
|
+
} from "./uuidUtilities";
|
|
34
|
+
export { SessionIdNormalizer } from "./sessionIdNormalizer";
|
|
35
|
+
export { fail, Mutable, getOrCreate, compareStrings } from "./utils";
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* eslint-disable no-bitwise */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
8
|
+
|
|
9
|
+
import { assert } from "@fluidframework/common-utils";
|
|
10
|
+
import { SessionId, StableId } from "@fluidframework/runtime-definitions";
|
|
11
|
+
import { fail } from "./utils";
|
|
12
|
+
import { generateStableId } from "./uuidUtilities";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A UUID (128 bit identifier) optimized for use as a 128 bit unsigned integer with fast addition and toString operations.
|
|
16
|
+
* The string entry is the upper 76 bits of the uuid and the integer entry holds the lower 52 bits:
|
|
17
|
+
*
|
|
18
|
+
* ```
|
|
19
|
+
* UUUUUUUU-UUUU-VUUU-vUUU-UUUUUUUUUUUU - the uuid
|
|
20
|
+
* SSSSSSSS-SSSS-SSSS-SSS - array[0]: string
|
|
21
|
+
* N NNNNNNNNNNNN - array[1]: integer
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* The integer keeps the common case cost of incrementing and computing deltas very low.
|
|
25
|
+
* The string optimizes toString by caching the the majority of the resulting string.
|
|
26
|
+
*/
|
|
27
|
+
export type NumericUuid = readonly [string, number] & {
|
|
28
|
+
readonly NumericUuid: "9132ea20-a811-4756-85f8-aa6da5ca90f8";
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const bitsInNumericUuidInteger = 52; // Not tunable. Do not change.
|
|
32
|
+
const nibblesInNumericUuidInteger = bitsInNumericUuidInteger / 4;
|
|
33
|
+
const stringEntryLength = 22;
|
|
34
|
+
const maxNumericUuidInteger = 2 ** bitsInNumericUuidInteger - 1;
|
|
35
|
+
const fiftyThirdBit = 2 ** 52;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Calculates the numeric delta between a and b (i.e. a - b).
|
|
39
|
+
* @param a - an uuid
|
|
40
|
+
* @param b - an other uuid
|
|
41
|
+
* @param maxDelta - the maximum integer delta (inclusive) to tolerate.
|
|
42
|
+
* @returns undefined if the delta is negative or greater than `maxDelta`
|
|
43
|
+
*/
|
|
44
|
+
export function getPositiveDelta(
|
|
45
|
+
a: NumericUuid,
|
|
46
|
+
b: NumericUuid,
|
|
47
|
+
maxDelta: number,
|
|
48
|
+
): number | undefined {
|
|
49
|
+
const [stringEntryA, lowNumberA] = a;
|
|
50
|
+
const [stringEntryB, lowNumberB] = b;
|
|
51
|
+
|
|
52
|
+
if (stringEntryA === stringEntryB) {
|
|
53
|
+
const difference = lowNumberA - lowNumberB;
|
|
54
|
+
if (difference >= 0 && difference <= maxDelta) {
|
|
55
|
+
return difference;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const highNumberA = Number.parseInt(ChunkMath.Upper.parse(stringEntryA), 16);
|
|
61
|
+
const highNumberB = Number.parseInt(ChunkMath.Upper.parse(stringEntryB), 16);
|
|
62
|
+
|
|
63
|
+
let subtractHigh = highNumberA - highNumberB;
|
|
64
|
+
if (Math.abs(subtractHigh) > 1) {
|
|
65
|
+
// If the high bits differ by more than 1, then there is no chance that any lower bits could compensate
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let midNumberA = ChunkMath.getNumericValue(ChunkMath.Variant.parse(stringEntryA));
|
|
70
|
+
const midNumberB = ChunkMath.getNumericValue(ChunkMath.Variant.parse(stringEntryB));
|
|
71
|
+
|
|
72
|
+
let subtractLow = lowNumberA - lowNumberB;
|
|
73
|
+
if (subtractLow < 0) {
|
|
74
|
+
midNumberA -= 1;
|
|
75
|
+
subtractLow += fiftyThirdBit;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let subtractMid = midNumberA - midNumberB;
|
|
79
|
+
if (subtractMid < 0) {
|
|
80
|
+
subtractHigh -= 1;
|
|
81
|
+
subtractMid += ChunkMath.twentyThirdBit;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (subtractHigh !== 0) {
|
|
85
|
+
// a < b, no positive delta, or
|
|
86
|
+
// a > b by much more than MAX_SAFE_INTEGER
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (subtractMid > 1) {
|
|
91
|
+
return undefined;
|
|
92
|
+
} else {
|
|
93
|
+
const trueDelta = fiftyThirdBit * subtractMid + subtractLow;
|
|
94
|
+
return trueDelta > maxDelta ? undefined : trueDelta;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Pre-allocated array of strings of zeros.
|
|
99
|
+
// Used to pad hex strings up to 52 bits
|
|
100
|
+
const zeros: string[] = [];
|
|
101
|
+
for (let i = 0; i < nibblesInNumericUuidInteger; i++) {
|
|
102
|
+
zeros.push("0".repeat(i));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function padToLengthWithZeros(str: string, count: number): string {
|
|
106
|
+
return str.length === count ? str : zeros[count - str.length] + str;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @param offset - an optional offset to increment the returned StableId
|
|
111
|
+
* @returns the string representation of a `NumericUuid`.
|
|
112
|
+
*/
|
|
113
|
+
export function stableIdFromNumericUuid(uuid: NumericUuid, offset = 0): StableId {
|
|
114
|
+
const lowerAdd = uuid[1] + offset;
|
|
115
|
+
// Common fast-path
|
|
116
|
+
if (lowerAdd <= maxNumericUuidInteger) {
|
|
117
|
+
const lowerString = padToLengthWithZeros(
|
|
118
|
+
lowerAdd.toString(16),
|
|
119
|
+
nibblesInNumericUuidInteger,
|
|
120
|
+
);
|
|
121
|
+
return `${uuid[0] + lowerString.slice(0, 1)}-${lowerString.slice(1)}` as StableId;
|
|
122
|
+
}
|
|
123
|
+
return stableIdFromNumericUuid(incrementUuid(uuid, offset));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param stableId - a minimal uuid string
|
|
128
|
+
* @returns a numeric representation of `stableId`.
|
|
129
|
+
*/
|
|
130
|
+
export function numericUuidFromStableId(stableId: StableId): NumericUuid {
|
|
131
|
+
const uuid: (string | number)[] = new Array(2);
|
|
132
|
+
uuid[0] = stableId.slice(0, stringEntryLength);
|
|
133
|
+
uuid[1] = Number.parseInt(ChunkMath.Lower.parse(stableId), 16);
|
|
134
|
+
return uuid as readonly (number | string)[] as NumericUuid;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Creates a session base ID.
|
|
139
|
+
* This method (rather than standard uuid generation methods) should be used to generate session IDs.
|
|
140
|
+
*/
|
|
141
|
+
export function createSessionId(): SessionId {
|
|
142
|
+
return ensureSessionUuid(generateStableId());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Compares numeric uuids for equality.
|
|
147
|
+
*/
|
|
148
|
+
export function numericUuidEquals(a: NumericUuid, b: NumericUuid): boolean {
|
|
149
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The maximum value that can be contained in the upper string region of a numeric UUID (i.e. the string region excluding the version
|
|
154
|
+
* nibble and the variant chunk)
|
|
155
|
+
*/
|
|
156
|
+
const maxUpperNumber = 2 ** 48 - 1;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Increments the uuid. `amount` must be a positive integer.
|
|
160
|
+
* @returns the result of incrementing the uuid by `amount`.
|
|
161
|
+
*/
|
|
162
|
+
export function incrementUuid(uuid: NumericUuid, amount: number): NumericUuid {
|
|
163
|
+
/*
|
|
164
|
+
* UUIDs incremented beyond the max UUID "ffffffff-ffff-4fff-bfff-ffffffffffff" will cause a failure.
|
|
165
|
+
* Also, some reserved bits of the v4 UUID must be treated as immutable (e.g. the version and
|
|
166
|
+
* variant bits) and thus must not be incremented.
|
|
167
|
+
*/
|
|
168
|
+
let newUuid: [string, number];
|
|
169
|
+
const result = uuid[1] + amount;
|
|
170
|
+
if (result <= maxNumericUuidInteger) {
|
|
171
|
+
// The new number still fits within the number region of a numeric UUID.
|
|
172
|
+
// Incrementing is usually done with small amounts, so this is the dominantly common case.
|
|
173
|
+
newUuid = [uuid[0], result];
|
|
174
|
+
} else {
|
|
175
|
+
// The numeric UUID's number region has overflowed. We will need to carry the overflow into the variant chunk (see `VariantChunk`).
|
|
176
|
+
/** The amount left over after filling up the rest of the uuid's number region with the increment amount */
|
|
177
|
+
const remainder = amount - (maxNumericUuidInteger - uuid[1]) - 1;
|
|
178
|
+
const stringEntry = uuid[0];
|
|
179
|
+
const [newVariantChunkString, carried] = ChunkMath.increment(stringEntry);
|
|
180
|
+
|
|
181
|
+
if (carried) {
|
|
182
|
+
// The variant chunk itself also overflowed. We'll need to carry the overflow further, into the upper string region of the UUID.
|
|
183
|
+
const upperString = ChunkMath.Upper.parse(stringEntry);
|
|
184
|
+
const upperNumber = Number.parseInt(upperString, 16);
|
|
185
|
+
assert(
|
|
186
|
+
upperNumber <= maxUpperNumber,
|
|
187
|
+
0x49c /* Unexpectedly large upper number when incrementing UUID */,
|
|
188
|
+
);
|
|
189
|
+
const newUpperNumber = upperNumber + 1;
|
|
190
|
+
if (newUpperNumber > maxUpperNumber) {
|
|
191
|
+
fail("Exceeded maximum numeric UUID");
|
|
192
|
+
} else {
|
|
193
|
+
// The variant chunk overflowed but the upper string region did not. Splice in the incremented string region.
|
|
194
|
+
const newUpperChunk = padToLengthWithZeros(newUpperNumber.toString(16), 12);
|
|
195
|
+
newUuid = [
|
|
196
|
+
`${ChunkMath.Upper.hyphenate(newUpperChunk)}-4${ChunkMath.Variant.hyphenate(
|
|
197
|
+
newVariantChunkString,
|
|
198
|
+
)}`,
|
|
199
|
+
remainder,
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
// The variant chunk did not overflow, so just splice it back in.
|
|
204
|
+
newUuid = [
|
|
205
|
+
`${ChunkMath.Upper.slice(stringEntry)}-4${ChunkMath.Variant.hyphenate(
|
|
206
|
+
newVariantChunkString,
|
|
207
|
+
)}`,
|
|
208
|
+
remainder,
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return newUuid as readonly [string, number] as NumericUuid;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
namespace ChunkMath {
|
|
217
|
+
/*
|
|
218
|
+
* Recall the UUID diagram from the top of this file which describes the layout of a Numeric UUID. To implement addition, we define
|
|
219
|
+
* another region called the "variant chunk" which overlaps with the "string" region. Note that it is just beneath the required v4 uuid
|
|
220
|
+
* version identifier (the 13th nibble 'V' which is always '4' in a v4 UUID) and just above the "number" region of the layout. It
|
|
221
|
+
* contains inside of it the v4 UUID variant identifier bits as well (see https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1).
|
|
222
|
+
*
|
|
223
|
+
* UUUUUUUU-UUUU-VUUU-vUUU-UUUUUUUUUUUU - the uuid
|
|
224
|
+
* SSSSSSSS SSSS SSSS SSS - array[0]: string
|
|
225
|
+
* N NNNNNNNNNNNN - array[1]: integer
|
|
226
|
+
* VVV-VVV - the variant chunk
|
|
227
|
+
*
|
|
228
|
+
* By defining the variant chunk in this way it is simple to splice in the v4 UUID version identifier ('V') just above it and any
|
|
229
|
+
* "bit math" required due to the fact that the variant identifier bits ('v') do not fill up an entire nibble is handled within it.
|
|
230
|
+
* The variant chunk is made up of 6 nibbles. Note the "vv" which denotes the two bits used for the v4 UUID variant identifier:
|
|
231
|
+
*
|
|
232
|
+
* AAAA BBBB CCCC vvDD EEEE FFFF
|
|
233
|
+
*
|
|
234
|
+
* Since we'll be needing to "skip" the variant bits ("vv") when doing addition, we define a a few masks which will be used below to
|
|
235
|
+
* separate the variant chunk into pieces before recombining it:
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
// AAAA BBBB CCCC vvDD EEEE FFFF
|
|
239
|
+
const upperVariantChunkMask = 0xfff000; // XXXX XXXX XXXX
|
|
240
|
+
const variantBitMask = 0x800; // XX
|
|
241
|
+
const middleVariantChunkMask = 0x300; // XX
|
|
242
|
+
const lowerVariantChunkMask = 0xff; // XXXX XXXX
|
|
243
|
+
|
|
244
|
+
/** The maximum numeric value that can be represented by the numerically relevant bits in the variant chunk */
|
|
245
|
+
const maxVariantNumber = 2 ** 22 - 1;
|
|
246
|
+
|
|
247
|
+
export const twentyThirdBit = 2 ** 22;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The upper chunk, denoted by 'U's in UUUUUUUU-UUUU-VVVV-vVVL-LLLLLLLLLLLL
|
|
251
|
+
*/
|
|
252
|
+
export namespace Upper {
|
|
253
|
+
export function parse(stringEntry: string): string {
|
|
254
|
+
return stringEntry.slice(0, 8) + stringEntry.slice(9, 13);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function hyphenate(upperChunk: string): string {
|
|
258
|
+
return `${upperChunk.slice(0, 8)}-${upperChunk.slice(8)}`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function slice(stringEntry: string): string {
|
|
262
|
+
return stringEntry.slice(0, 13);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* The variant chunk, denoted by 'V's in UUUUUUUU-UUUU-VVVV-vVVL-LLLLLLLLLLLL
|
|
268
|
+
*/
|
|
269
|
+
export namespace Variant {
|
|
270
|
+
export function parse(stringEntry: string): string {
|
|
271
|
+
return stringEntry.slice(15, 18) + stringEntry.slice(19, 22);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function hyphenate(variantChunk: string): string {
|
|
275
|
+
return `${variantChunk.slice(0, 3)}-${variantChunk.slice(3)}`;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The lower chunk, denoted by 'L's in UUUUUUUU-UUUU-VVVV-vVVL-LLLLLLLLLLLL
|
|
281
|
+
*/
|
|
282
|
+
export namespace Lower {
|
|
283
|
+
export function parse(stableId: StableId): string {
|
|
284
|
+
return (
|
|
285
|
+
stableId.slice(stringEntryLength, stringEntryLength + 1) +
|
|
286
|
+
stableId.slice(stringEntryLength + 2)
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function hyphenate(lowerChunk: string): string {
|
|
291
|
+
return `${lowerChunk.slice(0, 1)}-${lowerChunk.slice(1)}`;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Returns the number representation of the given bits corresponding to the variant chunk. The value is derived by
|
|
297
|
+
* parsing all bits except for reserved bits (i.e. the variant bits).
|
|
298
|
+
* @param variantChunk - the variantChunk
|
|
299
|
+
*/
|
|
300
|
+
export function getNumericValue(variantChunk: string): number {
|
|
301
|
+
const variantChunkBits = Number.parseInt(variantChunk, 16);
|
|
302
|
+
const upperVariantBits = (variantChunkBits & upperVariantChunkMask) >> 2;
|
|
303
|
+
const middleVariantBits = variantChunkBits & middleVariantChunkMask;
|
|
304
|
+
const lowerVariantBits = variantChunkBits & lowerVariantChunkMask;
|
|
305
|
+
return upperVariantBits + middleVariantBits + lowerVariantBits;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Given the string portion of a numeric uuid, add one to it.
|
|
310
|
+
* @returns the resulting hex string and whether or not the new value overflowed, i.e. it exceeds `maxVariantNumber`. In this case,
|
|
311
|
+
* the resulting hex string will wrap around to its minimum value '000b00'
|
|
312
|
+
*/
|
|
313
|
+
export function increment(stringEntry: string): [newVariantChunk: string, overflowed: boolean] {
|
|
314
|
+
// To implement addition, the variant identifier bits are extracted from the variant chunk, the chunk is interpreted as a number,
|
|
315
|
+
// that number is incremented by 1, and then the variant identifier bits are returned as we convert the number back into a hex
|
|
316
|
+
// string.
|
|
317
|
+
|
|
318
|
+
// This diagram may be helpful for seeing how the nibbles line up before and after the variant identifier bits are extracted. The
|
|
319
|
+
// letters used for each nibble ("AAAA", "BBBB") etc. are arbitrary and are simply there to differentiate the nibbles as they shift.
|
|
320
|
+
// --------------------------------
|
|
321
|
+
// 1. AAAA BBBB CCCC vvDD EEEE FFFF
|
|
322
|
+
// 2. AA AABB BBCC CCDD EEEE FFFF
|
|
323
|
+
// 3. AA AABB BBCC CCDD EEEE FFFF
|
|
324
|
+
// + 1
|
|
325
|
+
// = GG GGHH HHII IIJJ JJKK KKLL
|
|
326
|
+
// 4. GGGG HHHH IIII vvJJ JJKK KKLL
|
|
327
|
+
|
|
328
|
+
// 1. The variant chunk is given as a 6 character (6 nibble) hex string, where the fourth nibble contains the variant bits
|
|
329
|
+
// 2. The numerically important bits (i.e. not the variant identifier bits vv which are constant) are extracted into a single number
|
|
330
|
+
const variantChunk = Variant.parse(stringEntry);
|
|
331
|
+
const variantNumber = getNumericValue(variantChunk);
|
|
332
|
+
assert(
|
|
333
|
+
variantNumber <= maxVariantNumber,
|
|
334
|
+
0x49d /* Unexpectedly large variant number when incrementing UUID */,
|
|
335
|
+
);
|
|
336
|
+
// 3. Add one to the variant number to produce our new variant number.
|
|
337
|
+
const newVariantNumber = variantNumber + 1;
|
|
338
|
+
// 4. The variant identifier bits are added back into the number, which is then turned back into a hex string
|
|
339
|
+
const newUpperVariantBits = (newVariantNumber & (upperVariantChunkMask >> 2)) << 2;
|
|
340
|
+
const newMiddleVariantBits = (newVariantNumber & middleVariantChunkMask) | variantBitMask; // Add the variant bits back in
|
|
341
|
+
const newLowerVariantBits = newVariantNumber & lowerVariantChunkMask;
|
|
342
|
+
const newVariantChunkBits =
|
|
343
|
+
newUpperVariantBits + newMiddleVariantBits + newLowerVariantBits;
|
|
344
|
+
const newVariantChunk = padToLengthWithZeros(
|
|
345
|
+
newVariantChunkBits.toString(16),
|
|
346
|
+
variantChunk.length,
|
|
347
|
+
);
|
|
348
|
+
return [newVariantChunk, newVariantNumber > maxVariantNumber];
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const maxUpperUuid = "ffffffff-ffff-4fff-bf";
|
|
353
|
+
const maxNibbleCount = [...maxUpperUuid].filter((n) => n === "f").length;
|
|
354
|
+
const newNibbles = ["7", "b", "d", "e"];
|
|
355
|
+
function isMaxUpperNibble(index: number): boolean {
|
|
356
|
+
return maxUpperUuid.charAt(index) === "f";
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Any session uuid with all of its highish bits set is in danger of overflowing after fewer than 2^53 increments.
|
|
361
|
+
* By zeroing one of those bits at random, potential overflow is prevented.
|
|
362
|
+
*/
|
|
363
|
+
export function ensureSessionUuid(uuid: StableId): SessionId {
|
|
364
|
+
if (uuid.startsWith(maxUpperUuid)) {
|
|
365
|
+
const targetNibble = Math.floor(Math.random() * maxNibbleCount);
|
|
366
|
+
let actualIndex = 0;
|
|
367
|
+
for (
|
|
368
|
+
let nibbleIndex = 0;
|
|
369
|
+
nibbleIndex < targetNibble && !isMaxUpperNibble(actualIndex);
|
|
370
|
+
actualIndex += 1
|
|
371
|
+
) {
|
|
372
|
+
if (isMaxUpperNibble(actualIndex)) {
|
|
373
|
+
nibbleIndex++;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const newNibble = newNibbles[Math.floor(Math.random() * newNibbles.length)]; // Randomly choose a bit to zero
|
|
378
|
+
const newUuid = uuid.slice(0, actualIndex) + newNibble + uuid.slice(actualIndex + 1);
|
|
379
|
+
return newUuid as SessionId;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return uuid as SessionId;
|
|
383
|
+
}
|