@digitaldefiance/ecies-lib 4.10.8 → 4.11.1
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/README.md +4 -4
- package/package.json +1 -1
- package/src/enumerations/guid-brand-type.d.ts +1 -1
- package/src/enumerations/guid-brand-type.d.ts.map +1 -1
- package/src/enumerations/guid-brand-type.js +1 -1
- package/src/enumerations/guid-brand-type.js.map +1 -1
- package/src/errors/guid.d.ts +3 -3
- package/src/errors/guid.d.ts.map +1 -1
- package/src/errors/guid.js.map +1 -1
- package/src/examples/typed-configuration-usage.js +2 -2
- package/src/examples/typed-configuration-usage.js.map +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.d.ts.map +1 -1
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/interfaces/guid.d.ts +42 -8
- package/src/interfaces/guid.d.ts.map +1 -1
- package/src/interfaces/platform-id.d.ts +2 -2
- package/src/interfaces/platform-id.d.ts.map +1 -1
- package/src/lib/guid.d.ts +115 -77
- package/src/lib/guid.d.ts.map +1 -1
- package/src/lib/guid.js +342 -241
- package/src/lib/guid.js.map +1 -1
- package/src/lib/id-providers/guidv4-provider.d.ts +4 -3
- package/src/lib/id-providers/guidv4-provider.d.ts.map +1 -1
- package/src/lib/id-providers/guidv4-provider.js +22 -21
- package/src/lib/id-providers/guidv4-provider.js.map +1 -1
- package/src/typed-configuration.d.ts +1 -1
- package/src/typed-configuration.d.ts.map +1 -1
- package/src/types/guid-versions.d.ts +34 -0
- package/src/types/guid-versions.d.ts.map +1 -0
- package/src/types/guid-versions.js +7 -0
- package/src/types/guid-versions.js.map +1 -0
- package/src/types.d.ts +1 -1
- package/src/types.d.ts.map +1 -1
- package/src/utils.d.ts +1 -0
- package/src/utils.d.ts.map +1 -1
- package/src/utils.js +4 -0
- package/src/utils.js.map +1 -1
package/src/lib/guid.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Guid = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const uuid = tslib_1.__importStar(require("uuid"));
|
|
6
6
|
const guid_brand_type_1 = require("../enumerations/guid-brand-type");
|
|
7
7
|
const guid_error_type_1 = require("../enumerations/guid-error-type");
|
|
8
8
|
const guid_1 = require("../errors/guid");
|
|
9
|
-
const buffer_compat_1 = require("./buffer-compat");
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* Guid represents a GUID/UUID (Globally Unique Identifier) that is compliant with the RFC 4122 standard.
|
|
11
|
+
* Supports all UUID versions (v1-v5), with factory methods for v3, v4, and v5.
|
|
12
|
+
* Guid instances can be created from a variety of input types, including:
|
|
13
13
|
* - FullHexGuid: A 36-character string representation of the GUID, including dashes
|
|
14
14
|
* - ShortHexGuid: A 32-character string representation of the GUID, excluding dashes
|
|
15
15
|
* - Base64Guid: A 24-character base64-encoded string representation of the GUID
|
|
16
16
|
* - BigIntGuid: A bigint representation of the GUID
|
|
17
|
-
* -
|
|
18
|
-
*
|
|
17
|
+
* - RawGuidUint8Array: A 16-byte Uint8Array representation of the GUID
|
|
18
|
+
* Guid instances can be converted to any of these representations using the appropriate method.
|
|
19
19
|
*/
|
|
20
|
-
class
|
|
20
|
+
class Guid {
|
|
21
21
|
/**
|
|
22
|
-
* GUID is stored internally as a raw 16-byte
|
|
22
|
+
* GUID is stored internally as a raw 16-byte Uint8Array.
|
|
23
23
|
*/
|
|
24
24
|
_value;
|
|
25
25
|
/**
|
|
@@ -61,12 +61,6 @@ class GuidV4 {
|
|
|
61
61
|
static isUint8Array(value) {
|
|
62
62
|
return value instanceof Uint8Array;
|
|
63
63
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Type guard to check if a value is a Buffer or Uint8Array
|
|
66
|
-
*/
|
|
67
|
-
static isBufferLike(value) {
|
|
68
|
-
return buffer_compat_1.Buffer.isBuffer(value) || GuidV4.isUint8Array(value);
|
|
69
|
-
}
|
|
70
64
|
/**
|
|
71
65
|
* Cached empty/nil GUID constant (all zeros)
|
|
72
66
|
*/
|
|
@@ -75,16 +69,16 @@ class GuidV4 {
|
|
|
75
69
|
* Empty/nil GUID constant (all zeros)
|
|
76
70
|
*/
|
|
77
71
|
static get Empty() {
|
|
78
|
-
if (!
|
|
79
|
-
|
|
72
|
+
if (!Guid._empty) {
|
|
73
|
+
Guid._empty = Object.freeze(new Guid('00000000-0000-0000-0000-000000000000'));
|
|
80
74
|
}
|
|
81
|
-
return
|
|
75
|
+
return Guid._empty;
|
|
82
76
|
}
|
|
83
77
|
constructor(value) {
|
|
84
|
-
const
|
|
78
|
+
const array = Guid.validateAndConvert(value);
|
|
85
79
|
// Note: We cannot freeze a Buffer as it's an ArrayBuffer view
|
|
86
|
-
// Instead, we ensure the
|
|
87
|
-
this._value =
|
|
80
|
+
// Instead, we ensure the array is never directly modified after construction
|
|
81
|
+
this._value = array;
|
|
88
82
|
// Initialize cache properties so they exist before sealing
|
|
89
83
|
this._cachedFullHex = undefined;
|
|
90
84
|
this._cachedShortHex = undefined;
|
|
@@ -94,10 +88,10 @@ class GuidV4 {
|
|
|
94
88
|
Object.seal(this);
|
|
95
89
|
}
|
|
96
90
|
/**
|
|
97
|
-
* Validates input and converts to raw
|
|
91
|
+
* Validates input and converts to raw array with comprehensive error handling.
|
|
98
92
|
* This centralizes all validation logic for better maintainability.
|
|
99
93
|
* @param value The input value to validate and convert
|
|
100
|
-
* @returns The validated raw GUID
|
|
94
|
+
* @returns The validated raw GUID array
|
|
101
95
|
* @throws {GuidError} If validation fails
|
|
102
96
|
*/
|
|
103
97
|
static validateAndConvert(value) {
|
|
@@ -119,42 +113,36 @@ class GuidV4 {
|
|
|
119
113
|
if (typeof value === 'string') {
|
|
120
114
|
const isFullHex = value.length === 36 && value.includes('-');
|
|
121
115
|
const isShortHex = value.length === 32 && !value.includes('-');
|
|
122
|
-
if (isFullHex && !
|
|
123
|
-
const
|
|
124
|
-
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidWithDetails, guid_brand_type_1.GuidBrandType.FullHexGuid, value.length,
|
|
116
|
+
if (isFullHex && !Guid.FULL_HEX_PATTERN.test(value)) {
|
|
117
|
+
const array = new TextEncoder().encode(value);
|
|
118
|
+
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidWithDetails, guid_brand_type_1.GuidBrandType.FullHexGuid, value.length, array);
|
|
125
119
|
}
|
|
126
|
-
else if (isShortHex && !
|
|
127
|
-
const
|
|
128
|
-
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidWithDetails, guid_brand_type_1.GuidBrandType.ShortHexGuid, value.length,
|
|
120
|
+
else if (isShortHex && !Guid.HEX_PATTERN.test(value)) {
|
|
121
|
+
const array = new TextEncoder().encode(value);
|
|
122
|
+
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidWithDetails, guid_brand_type_1.GuidBrandType.ShortHexGuid, value.length, array);
|
|
129
123
|
}
|
|
130
124
|
}
|
|
131
125
|
// Determine and verify the brand/type
|
|
132
|
-
const expectedBrand =
|
|
133
|
-
const verifiedBrand =
|
|
126
|
+
const expectedBrand = Guid.whichBrand(value);
|
|
127
|
+
const verifiedBrand = Guid.verifyGuid(expectedBrand, value);
|
|
134
128
|
if (!verifiedBrand) {
|
|
135
|
-
const valueBuffer =
|
|
129
|
+
const valueBuffer = Guid.isUint8Array(value)
|
|
136
130
|
? value
|
|
137
|
-
:
|
|
131
|
+
: new TextEncoder().encode(strValue);
|
|
138
132
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidWithDetails, expectedBrand, undefined, valueBuffer);
|
|
139
133
|
}
|
|
140
|
-
// Convert to raw
|
|
141
|
-
const
|
|
134
|
+
// Convert to raw array
|
|
135
|
+
const array = Guid.toRawGuidPlatformBuffer(value);
|
|
142
136
|
// Validate against UUID standard (skip for boundary values)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
hexString = buffer.toString('hex');
|
|
151
|
-
}
|
|
152
|
-
const fullHex = GuidV4.toFullHexGuid(hexString);
|
|
153
|
-
const isBoundary = GuidV4.isBoundaryValue(fullHex);
|
|
137
|
+
const hexString = Array.from(array)
|
|
138
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
139
|
+
.join('');
|
|
140
|
+
const fullHex = Guid.toFullHexGuid(hexString);
|
|
141
|
+
const isBoundary = Guid.isBoundaryValue(fullHex);
|
|
154
142
|
if (!isBoundary && !uuid.validate(fullHex)) {
|
|
155
|
-
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid, expectedBrand, undefined,
|
|
143
|
+
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid, expectedBrand, undefined, array);
|
|
156
144
|
}
|
|
157
|
-
return
|
|
145
|
+
return array;
|
|
158
146
|
}
|
|
159
147
|
catch (error) {
|
|
160
148
|
// Re-throw GuidError as-is
|
|
@@ -165,7 +153,7 @@ class GuidV4 {
|
|
|
165
153
|
if (typeof value === 'bigint') {
|
|
166
154
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
167
155
|
}
|
|
168
|
-
const length =
|
|
156
|
+
const length = Guid.isUint8Array(value)
|
|
169
157
|
? value.length
|
|
170
158
|
: String(value).length;
|
|
171
159
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownLength, undefined, length);
|
|
@@ -178,14 +166,14 @@ class GuidV4 {
|
|
|
178
166
|
return this.asBase64Guid;
|
|
179
167
|
}
|
|
180
168
|
static hydrate(value) {
|
|
181
|
-
return new
|
|
169
|
+
return new Guid(value);
|
|
182
170
|
}
|
|
183
171
|
static LengthMap = {
|
|
184
172
|
[guid_brand_type_1.GuidBrandType.Unknown]: -1,
|
|
185
173
|
[guid_brand_type_1.GuidBrandType.FullHexGuid]: 36,
|
|
186
174
|
[guid_brand_type_1.GuidBrandType.ShortHexGuid]: 32,
|
|
187
175
|
[guid_brand_type_1.GuidBrandType.Base64Guid]: 24,
|
|
188
|
-
[guid_brand_type_1.GuidBrandType.
|
|
176
|
+
[guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer]: 16,
|
|
189
177
|
[guid_brand_type_1.GuidBrandType.BigIntGuid]: -1, // Variable length
|
|
190
178
|
};
|
|
191
179
|
static ReverseLengthMap = {
|
|
@@ -193,7 +181,7 @@ class GuidV4 {
|
|
|
193
181
|
36: guid_brand_type_1.GuidBrandType.FullHexGuid,
|
|
194
182
|
32: guid_brand_type_1.GuidBrandType.ShortHexGuid,
|
|
195
183
|
24: guid_brand_type_1.GuidBrandType.Base64Guid,
|
|
196
|
-
16: guid_brand_type_1.GuidBrandType.
|
|
184
|
+
16: guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer,
|
|
197
185
|
// BigIntGuid is variable length, so it is not included in the reverse map
|
|
198
186
|
};
|
|
199
187
|
static VerifyFunctions = {
|
|
@@ -202,28 +190,28 @@ class GuidV4 {
|
|
|
202
190
|
[guid_brand_type_1.GuidBrandType.ShortHexGuid]: (guid) => this.isShortHexGuid(guid),
|
|
203
191
|
[guid_brand_type_1.GuidBrandType.Base64Guid]: (guid) => this.isBase64Guid(guid),
|
|
204
192
|
[guid_brand_type_1.GuidBrandType.BigIntGuid]: (guid) => this.isBigIntGuid(guid),
|
|
205
|
-
[guid_brand_type_1.GuidBrandType.
|
|
193
|
+
[guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer]: (guid) => this.isRawGuidUint8Array(guid),
|
|
206
194
|
};
|
|
207
195
|
/**
|
|
208
|
-
* Returns the GUID as a raw
|
|
196
|
+
* Returns the GUID as a raw Uint8Array.
|
|
209
197
|
* NOTE: Returns a defensive copy to prevent external mutation.
|
|
210
|
-
* Use
|
|
198
|
+
* Use asRawGuidUint8ArrayUnsafe() if you need the internal array and guarantee no mutation.
|
|
211
199
|
*/
|
|
212
|
-
get
|
|
213
|
-
return
|
|
200
|
+
get asRawGuidPlatformBuffer() {
|
|
201
|
+
return new Uint8Array(this._value);
|
|
214
202
|
}
|
|
215
203
|
/**
|
|
216
|
-
* Returns the internal raw
|
|
217
|
-
* ⚠️ WARNING: Do NOT mutate the returned
|
|
218
|
-
* Mutating this
|
|
204
|
+
* Returns the internal raw Uint8Array without copying.
|
|
205
|
+
* ⚠️ WARNING: Do NOT mutate the returned array! This is for performance-critical paths only.
|
|
206
|
+
* Mutating this array will corrupt the GUID instance.
|
|
219
207
|
* @internal
|
|
220
208
|
*/
|
|
221
|
-
get
|
|
209
|
+
get asRawGuidPlatformBufferUnsafe() {
|
|
222
210
|
return this._value;
|
|
223
211
|
}
|
|
224
212
|
/**
|
|
225
213
|
* Generates a new random v4 GUID.
|
|
226
|
-
* @returns A new
|
|
214
|
+
* @returns A new Guid instance with a randomly generated value
|
|
227
215
|
*/
|
|
228
216
|
static generate() {
|
|
229
217
|
try {
|
|
@@ -231,7 +219,7 @@ class GuidV4 {
|
|
|
231
219
|
if (!uuidStr) {
|
|
232
220
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
233
221
|
}
|
|
234
|
-
return new
|
|
222
|
+
return new Guid(uuidStr);
|
|
235
223
|
}
|
|
236
224
|
catch (error) {
|
|
237
225
|
if (error instanceof guid_1.GuidError) {
|
|
@@ -240,32 +228,39 @@ class GuidV4 {
|
|
|
240
228
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
241
229
|
}
|
|
242
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Alias for generate() to create a v4 GUID.
|
|
233
|
+
* @returns A new Guid instance with a randomly generated v4 value
|
|
234
|
+
*/
|
|
235
|
+
static v4() {
|
|
236
|
+
return Guid.generate();
|
|
237
|
+
}
|
|
243
238
|
/**
|
|
244
239
|
* Alias for generate() for backward compatibility.
|
|
245
240
|
* @deprecated Use generate() instead for clearer intent
|
|
246
241
|
*/
|
|
247
242
|
static new() {
|
|
248
|
-
return
|
|
243
|
+
return Guid.generate();
|
|
249
244
|
}
|
|
250
245
|
/**
|
|
251
246
|
* Parses a GUID from any valid format, throwing on invalid input.
|
|
252
247
|
* This is the primary parsing method for when you expect valid input.
|
|
253
248
|
* @param value The value to parse
|
|
254
|
-
* @returns A new
|
|
249
|
+
* @returns A new Guid instance
|
|
255
250
|
* @throws {GuidError} If the value is not a valid GUID
|
|
256
251
|
*/
|
|
257
252
|
static parse(value) {
|
|
258
|
-
return new
|
|
253
|
+
return new Guid(value);
|
|
259
254
|
}
|
|
260
255
|
/**
|
|
261
256
|
* Attempts to parse a GUID, returning null on failure instead of throwing.
|
|
262
257
|
* Use this when you're uncertain if the input is valid.
|
|
263
258
|
* @param value The value to parse
|
|
264
|
-
* @returns A new
|
|
259
|
+
* @returns A new Guid instance or null if parsing fails
|
|
265
260
|
*/
|
|
266
261
|
static tryParse(value) {
|
|
267
262
|
try {
|
|
268
|
-
return new
|
|
263
|
+
return new Guid(value);
|
|
269
264
|
}
|
|
270
265
|
catch {
|
|
271
266
|
return null;
|
|
@@ -281,7 +276,7 @@ class GuidV4 {
|
|
|
281
276
|
if (!value)
|
|
282
277
|
return false;
|
|
283
278
|
try {
|
|
284
|
-
const guid = new
|
|
279
|
+
const guid = new Guid(value);
|
|
285
280
|
return guid.isValidV4();
|
|
286
281
|
}
|
|
287
282
|
catch {
|
|
@@ -291,69 +286,61 @@ class GuidV4 {
|
|
|
291
286
|
/**
|
|
292
287
|
* Factory method to create a GUID from a full hex string.
|
|
293
288
|
* @param fullHex The full hex string (with dashes)
|
|
294
|
-
* @returns A new
|
|
289
|
+
* @returns A new Guid instance
|
|
295
290
|
*/
|
|
296
291
|
static fromFullHex(fullHex) {
|
|
297
|
-
return new
|
|
292
|
+
return new Guid(fullHex);
|
|
298
293
|
}
|
|
299
294
|
/**
|
|
300
295
|
* Factory method to create a GUID from a short hex string.
|
|
301
296
|
* @param shortHex The short hex string (without dashes)
|
|
302
|
-
* @returns A new
|
|
297
|
+
* @returns A new Guid instance
|
|
303
298
|
*/
|
|
304
299
|
static fromShortHex(shortHex) {
|
|
305
|
-
return new
|
|
300
|
+
return new Guid(shortHex);
|
|
306
301
|
}
|
|
307
302
|
/**
|
|
308
303
|
* Factory method to create a GUID from a base64 string.
|
|
309
304
|
* @param base64 The base64 encoded string
|
|
310
|
-
* @returns A new
|
|
305
|
+
* @returns A new Guid instance
|
|
311
306
|
*/
|
|
312
307
|
static fromBase64(base64) {
|
|
313
|
-
return new
|
|
308
|
+
return new Guid(base64);
|
|
314
309
|
}
|
|
315
310
|
/**
|
|
316
311
|
* Factory method to create a GUID from a bigint.
|
|
317
312
|
* @param bigint The bigint value
|
|
318
|
-
* @returns A new
|
|
313
|
+
* @returns A new Guid instance
|
|
319
314
|
*/
|
|
320
315
|
static fromBigInt(bigint) {
|
|
321
|
-
return new
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Factory method to create a GUID from a raw buffer.
|
|
325
|
-
* @param buffer The raw 16-byte buffer
|
|
326
|
-
* @returns A new GuidV4 instance
|
|
327
|
-
*/
|
|
328
|
-
static fromBuffer(buffer) {
|
|
329
|
-
return new GuidV4(buffer);
|
|
316
|
+
return new Guid(bigint);
|
|
330
317
|
}
|
|
331
318
|
/**
|
|
332
319
|
* Factory method to create a GUID from a raw Uint8Array.
|
|
333
320
|
* This is an explicit alias for fromBuffer(), provided for clarity when working
|
|
334
321
|
* with browser environments where Uint8Array is the native binary type.
|
|
335
322
|
* @param bytes The raw 16-byte Uint8Array
|
|
336
|
-
* @returns A new
|
|
323
|
+
* @returns A new Guid instance
|
|
337
324
|
*/
|
|
338
|
-
static
|
|
339
|
-
return new
|
|
325
|
+
static fromPlatformBuffer(bytes) {
|
|
326
|
+
return new Guid(bytes);
|
|
340
327
|
}
|
|
341
328
|
/**
|
|
342
329
|
* Creates a namespace-based v3 GUID (MD5 hash).
|
|
343
330
|
* Use this for deterministic GUIDs based on a namespace and name.
|
|
344
|
-
* @param namespace The namespace GUID (e.g., uuid.v3.DNS)
|
|
345
331
|
* @param name The name to hash within the namespace
|
|
346
|
-
* @
|
|
332
|
+
* @param namespace The namespace GUID (e.g., Guid.Namespaces.DNS)
|
|
333
|
+
* @returns A new Guid instance containing the v3 GUID
|
|
347
334
|
* @example
|
|
348
|
-
* const guid =
|
|
335
|
+
* const guid = Guid.v3('example.com', Guid.Namespaces.DNS);
|
|
349
336
|
*/
|
|
350
337
|
static v3(name, namespace) {
|
|
351
338
|
try {
|
|
352
|
-
const namespaceStr =
|
|
353
|
-
?
|
|
354
|
-
: namespace;
|
|
339
|
+
const namespaceStr = typeof namespace === 'string'
|
|
340
|
+
? namespace
|
|
341
|
+
: Guid.toFullHexGuid(namespace);
|
|
355
342
|
const v3Guid = uuid.v3(name, namespaceStr);
|
|
356
|
-
return new
|
|
343
|
+
return new Guid(v3Guid);
|
|
357
344
|
}
|
|
358
345
|
catch (error) {
|
|
359
346
|
if (error instanceof guid_1.GuidError) {
|
|
@@ -366,19 +353,19 @@ class GuidV4 {
|
|
|
366
353
|
* Creates a namespace-based v5 GUID (SHA-1 hash).
|
|
367
354
|
* Use this for deterministic GUIDs based on a namespace and name.
|
|
368
355
|
* Preferred over v3 as SHA-1 is stronger than MD5.
|
|
369
|
-
* @param namespace The namespace GUID (e.g., uuid.v5.DNS)
|
|
370
356
|
* @param name The name to hash within the namespace
|
|
371
|
-
* @
|
|
357
|
+
* @param namespace The namespace GUID (e.g., Guid.Namespaces.DNS)
|
|
358
|
+
* @returns A new Guid instance containing the v5 GUID
|
|
372
359
|
* @example
|
|
373
|
-
* const guid =
|
|
360
|
+
* const guid = Guid.v5('example.com', Guid.Namespaces.DNS);
|
|
374
361
|
*/
|
|
375
362
|
static v5(name, namespace) {
|
|
376
363
|
try {
|
|
377
|
-
const namespaceStr =
|
|
378
|
-
?
|
|
379
|
-
: namespace;
|
|
364
|
+
const namespaceStr = typeof namespace === 'string'
|
|
365
|
+
? namespace
|
|
366
|
+
: Guid.toFullHexGuid(namespace);
|
|
380
367
|
const v5Guid = uuid.v5(name, namespaceStr);
|
|
381
|
-
return new
|
|
368
|
+
return new Guid(v5Guid);
|
|
382
369
|
}
|
|
383
370
|
catch (error) {
|
|
384
371
|
if (error instanceof guid_1.GuidError) {
|
|
@@ -404,23 +391,17 @@ class GuidV4 {
|
|
|
404
391
|
*/
|
|
405
392
|
get asFullHexGuid() {
|
|
406
393
|
if (!this._cachedFullHex) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
.join('');
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
hexString = this._value.toString('hex');
|
|
415
|
-
}
|
|
416
|
-
this._cachedFullHex = GuidV4.toFullHexGuid(hexString);
|
|
394
|
+
const hexString = Array.from(this._value)
|
|
395
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
396
|
+
.join('');
|
|
397
|
+
this._cachedFullHex = Guid.toFullHexGuid(hexString);
|
|
417
398
|
}
|
|
418
399
|
return this._cachedFullHex;
|
|
419
400
|
}
|
|
420
401
|
/**
|
|
421
402
|
* Returns the GUID as a raw Uint8Array.
|
|
422
403
|
*/
|
|
423
|
-
get
|
|
404
|
+
get asPlatformBuffer() {
|
|
424
405
|
return this._value;
|
|
425
406
|
}
|
|
426
407
|
/**
|
|
@@ -429,7 +410,7 @@ class GuidV4 {
|
|
|
429
410
|
*/
|
|
430
411
|
get asShortHexGuid() {
|
|
431
412
|
if (!this._cachedShortHex) {
|
|
432
|
-
this._cachedShortHex =
|
|
413
|
+
this._cachedShortHex = Guid.toShortHexGuid(this.asFullHexGuid);
|
|
433
414
|
}
|
|
434
415
|
return this._cachedShortHex;
|
|
435
416
|
}
|
|
@@ -450,7 +431,10 @@ class GuidV4 {
|
|
|
450
431
|
* Returns the GUID as a bigint.
|
|
451
432
|
*/
|
|
452
433
|
get asBigIntGuid() {
|
|
453
|
-
|
|
434
|
+
const hexString = Array.from(this._value)
|
|
435
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
436
|
+
.join('');
|
|
437
|
+
return BigInt('0x' + hexString);
|
|
454
438
|
}
|
|
455
439
|
/**
|
|
456
440
|
* Returns the GUID as a base64 string.
|
|
@@ -458,7 +442,7 @@ class GuidV4 {
|
|
|
458
442
|
*/
|
|
459
443
|
get asBase64Guid() {
|
|
460
444
|
if (!this._cachedBase64) {
|
|
461
|
-
this._cachedBase64 = this._value
|
|
445
|
+
this._cachedBase64 = btoa(String.fromCharCode(...this._value));
|
|
462
446
|
}
|
|
463
447
|
return this._cachedBase64;
|
|
464
448
|
}
|
|
@@ -468,10 +452,10 @@ class GuidV4 {
|
|
|
468
452
|
* @returns True if the value is a boundary value.
|
|
469
453
|
*/
|
|
470
454
|
static isBoundaryValue(value) {
|
|
471
|
-
return (value ===
|
|
472
|
-
value ===
|
|
473
|
-
value ===
|
|
474
|
-
value ===
|
|
455
|
+
return (value === Guid.BOUNDARY_VALUES.ALL_ZEROS_FULL ||
|
|
456
|
+
value === Guid.BOUNDARY_VALUES.ALL_ZEROS_SHORT ||
|
|
457
|
+
value === Guid.BOUNDARY_VALUES.ALL_FS_FULL ||
|
|
458
|
+
value === Guid.BOUNDARY_VALUES.ALL_FS_SHORT);
|
|
475
459
|
}
|
|
476
460
|
/**
|
|
477
461
|
* Verifies if a given GUID is valid for the given brand.
|
|
@@ -484,7 +468,7 @@ class GuidV4 {
|
|
|
484
468
|
return false;
|
|
485
469
|
}
|
|
486
470
|
try {
|
|
487
|
-
const verifyFunc =
|
|
471
|
+
const verifyFunc = Guid.VerifyFunctions[guidBrand];
|
|
488
472
|
return verifyFunc(guid);
|
|
489
473
|
}
|
|
490
474
|
catch {
|
|
@@ -497,7 +481,7 @@ class GuidV4 {
|
|
|
497
481
|
* @returns The length of the GUID for the given brand.
|
|
498
482
|
*/
|
|
499
483
|
static guidBrandToLength(guidBrand) {
|
|
500
|
-
const length =
|
|
484
|
+
const length = Guid.LengthMap[guidBrand];
|
|
501
485
|
if (length <= 0) {
|
|
502
486
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownBrand, guidBrand);
|
|
503
487
|
}
|
|
@@ -506,20 +490,20 @@ class GuidV4 {
|
|
|
506
490
|
/**
|
|
507
491
|
* Returns the brand of the GUID for the given length.
|
|
508
492
|
* @param length The length of the GUID to get the brand for.
|
|
509
|
-
* @param
|
|
493
|
+
* @param isUint8Array Whether the GUID is a Uint8Array.
|
|
510
494
|
* @returns The brand of the GUID for the given length.
|
|
511
495
|
*/
|
|
512
|
-
static lengthToGuidBrand(length,
|
|
496
|
+
static lengthToGuidBrand(length, isUint8Array) {
|
|
513
497
|
if (length <= 0) {
|
|
514
498
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownLength, undefined, length);
|
|
515
499
|
}
|
|
516
|
-
const brand =
|
|
500
|
+
const brand = Guid.ReverseLengthMap[length];
|
|
517
501
|
if (!brand || brand === guid_brand_type_1.GuidBrandType.Unknown) {
|
|
518
502
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownLength, undefined, length);
|
|
519
503
|
}
|
|
520
|
-
// Validate
|
|
521
|
-
const
|
|
522
|
-
if (
|
|
504
|
+
// Validate array vs string type consistency
|
|
505
|
+
const isBrandUint8Array = brand === guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer;
|
|
506
|
+
if (isUint8Array !== isBrandUint8Array) {
|
|
523
507
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownLength, brand, length);
|
|
524
508
|
}
|
|
525
509
|
return brand;
|
|
@@ -534,16 +518,16 @@ class GuidV4 {
|
|
|
534
518
|
if (fullHexGuidValue === null || fullHexGuidValue === undefined) {
|
|
535
519
|
return false;
|
|
536
520
|
}
|
|
537
|
-
const expectedLength =
|
|
521
|
+
const expectedLength = Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.FullHexGuid);
|
|
538
522
|
const strValue = String(fullHexGuidValue);
|
|
539
523
|
if (strValue.length !== expectedLength) {
|
|
540
524
|
return false;
|
|
541
525
|
}
|
|
542
526
|
// Boundary values are always valid
|
|
543
|
-
if (
|
|
527
|
+
if (Guid.isBoundaryValue(strValue)) {
|
|
544
528
|
return true;
|
|
545
529
|
}
|
|
546
|
-
return
|
|
530
|
+
return Guid.validateUuid(strValue);
|
|
547
531
|
}
|
|
548
532
|
catch {
|
|
549
533
|
return false;
|
|
@@ -559,15 +543,15 @@ class GuidV4 {
|
|
|
559
543
|
if (shortHexGuidValue === null || shortHexGuidValue === undefined) {
|
|
560
544
|
return false;
|
|
561
545
|
}
|
|
562
|
-
const expectedLength =
|
|
546
|
+
const expectedLength = Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.ShortHexGuid);
|
|
563
547
|
const strValue = String(shortHexGuidValue);
|
|
564
548
|
if (strValue.length !== expectedLength) {
|
|
565
549
|
return false;
|
|
566
550
|
}
|
|
567
551
|
try {
|
|
568
|
-
const fullHexGuid =
|
|
552
|
+
const fullHexGuid = Guid.toFullHexGuid(strValue);
|
|
569
553
|
// Boundary values are always valid
|
|
570
|
-
if (
|
|
554
|
+
if (Guid.isBoundaryValue(fullHexGuid)) {
|
|
571
555
|
return true;
|
|
572
556
|
}
|
|
573
557
|
return uuid.validate(fullHexGuid);
|
|
@@ -594,28 +578,22 @@ class GuidV4 {
|
|
|
594
578
|
if (typeof value === 'bigint') {
|
|
595
579
|
valueLength = value.toString(16).length;
|
|
596
580
|
}
|
|
597
|
-
else if (
|
|
581
|
+
else if (Guid.isUint8Array(value)) {
|
|
598
582
|
valueLength = value.length;
|
|
599
583
|
}
|
|
600
584
|
else {
|
|
601
585
|
valueLength = String(value).length;
|
|
602
586
|
}
|
|
603
|
-
const result = valueLength ===
|
|
587
|
+
const result = valueLength === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.Base64Guid);
|
|
604
588
|
if (result) {
|
|
605
589
|
try {
|
|
606
|
-
const fromBase64 =
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
.join('');
|
|
612
|
-
}
|
|
613
|
-
else {
|
|
614
|
-
hexString = fromBase64.toString('hex');
|
|
615
|
-
}
|
|
616
|
-
const fullHexGuid = GuidV4.toFullHexGuid(hexString);
|
|
590
|
+
const fromBase64 = Guid.toRawGuidPlatformBuffer(value);
|
|
591
|
+
const hexString = Array.from(fromBase64)
|
|
592
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
593
|
+
.join('');
|
|
594
|
+
const fullHexGuid = Guid.toFullHexGuid(hexString);
|
|
617
595
|
// Boundary values are always valid
|
|
618
|
-
if (
|
|
596
|
+
if (Guid.isBoundaryValue(fullHexGuid)) {
|
|
619
597
|
return true;
|
|
620
598
|
}
|
|
621
599
|
return uuid.validate(fullHexGuid);
|
|
@@ -631,21 +609,21 @@ class GuidV4 {
|
|
|
631
609
|
}
|
|
632
610
|
}
|
|
633
611
|
/**
|
|
634
|
-
* Verifies if a given GUID is a valid raw GUID
|
|
635
|
-
* @param value The raw GUID
|
|
636
|
-
* @returns True if the GUID is a valid raw GUID
|
|
612
|
+
* Verifies if a given GUID is a valid raw GUID array.
|
|
613
|
+
* @param value The raw GUID array to verify.
|
|
614
|
+
* @returns True if the GUID is a valid raw GUID array, false otherwise.
|
|
637
615
|
*/
|
|
638
|
-
static
|
|
616
|
+
static isRawGuidUint8Array(value) {
|
|
639
617
|
try {
|
|
640
618
|
if (value === null || value === undefined) {
|
|
641
619
|
return false;
|
|
642
620
|
}
|
|
643
|
-
const expectedLength =
|
|
621
|
+
const expectedLength = Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer);
|
|
644
622
|
let valueLength;
|
|
645
623
|
if (typeof value === 'bigint') {
|
|
646
624
|
valueLength = value.toString(16).length;
|
|
647
625
|
}
|
|
648
|
-
else if (
|
|
626
|
+
else if (Guid.isUint8Array(value)) {
|
|
649
627
|
valueLength = value.length;
|
|
650
628
|
}
|
|
651
629
|
else {
|
|
@@ -655,24 +633,18 @@ class GuidV4 {
|
|
|
655
633
|
return false;
|
|
656
634
|
}
|
|
657
635
|
try {
|
|
658
|
-
if (!
|
|
636
|
+
if (!Guid.isUint8Array(value)) {
|
|
659
637
|
return false;
|
|
660
638
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
.join('');
|
|
666
|
-
}
|
|
667
|
-
else {
|
|
668
|
-
hexString = value.toString('hex');
|
|
669
|
-
}
|
|
670
|
-
const fullHexGuid = GuidV4.toFullHexGuid(hexString);
|
|
639
|
+
const hexString = Array.from(value)
|
|
640
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
641
|
+
.join('');
|
|
642
|
+
const fullHexGuid = Guid.toFullHexGuid(hexString);
|
|
671
643
|
// Boundary values are always valid
|
|
672
|
-
if (
|
|
644
|
+
if (Guid.isBoundaryValue(fullHexGuid)) {
|
|
673
645
|
return true;
|
|
674
646
|
}
|
|
675
|
-
return
|
|
647
|
+
return Guid.validateUuid(fullHexGuid);
|
|
676
648
|
}
|
|
677
649
|
catch {
|
|
678
650
|
return false;
|
|
@@ -695,13 +667,13 @@ class GuidV4 {
|
|
|
695
667
|
if (typeof value !== 'bigint') {
|
|
696
668
|
return false;
|
|
697
669
|
}
|
|
698
|
-
if (value < 0n || value >
|
|
670
|
+
if (value < 0n || value > Guid.MAX_BIGINT_VALUE) {
|
|
699
671
|
return false;
|
|
700
672
|
}
|
|
701
673
|
try {
|
|
702
|
-
const fromBigInt =
|
|
674
|
+
const fromBigInt = Guid.toFullHexFromBigInt(value);
|
|
703
675
|
// Boundary values are always valid
|
|
704
|
-
if (
|
|
676
|
+
if (Guid.isBoundaryValue(fromBigInt)) {
|
|
705
677
|
return true;
|
|
706
678
|
}
|
|
707
679
|
return uuid.validate(fromBigInt);
|
|
@@ -726,11 +698,11 @@ class GuidV4 {
|
|
|
726
698
|
if (typeof value === 'bigint') {
|
|
727
699
|
return guid_brand_type_1.GuidBrandType.BigIntGuid;
|
|
728
700
|
}
|
|
729
|
-
const isBuffer =
|
|
701
|
+
const isBuffer = Guid.isUint8Array(value);
|
|
730
702
|
const expectedLength = isBuffer
|
|
731
703
|
? value.length
|
|
732
704
|
: String(value).length;
|
|
733
|
-
return
|
|
705
|
+
return Guid.lengthToGuidBrand(expectedLength, isBuffer);
|
|
734
706
|
}
|
|
735
707
|
/**
|
|
736
708
|
* Converts a given short hex GUID to a full hex GUID.
|
|
@@ -752,39 +724,42 @@ class GuidV4 {
|
|
|
752
724
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
753
725
|
}
|
|
754
726
|
if (typeof guid === 'bigint') {
|
|
755
|
-
return
|
|
727
|
+
return Guid.toFullHexFromBigInt(guid);
|
|
756
728
|
}
|
|
757
|
-
else if (
|
|
758
|
-
guid.length ===
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
.join('');
|
|
764
|
-
}
|
|
765
|
-
else {
|
|
766
|
-
hexString = guid.toString('hex');
|
|
767
|
-
}
|
|
729
|
+
else if (Guid.isUint8Array(guid) &&
|
|
730
|
+
guid.length ===
|
|
731
|
+
Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer)) {
|
|
732
|
+
const hexString = Array.from(guid)
|
|
733
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
734
|
+
.join('');
|
|
768
735
|
const shortHex = hexString;
|
|
769
|
-
return
|
|
736
|
+
return Guid.shortGuidToFullGuid(shortHex);
|
|
770
737
|
}
|
|
771
|
-
else if (
|
|
738
|
+
else if (Guid.isUint8Array(guid)) {
|
|
772
739
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
773
740
|
}
|
|
774
741
|
// all remaining cases are string types
|
|
775
742
|
const strValue = String(guid);
|
|
776
|
-
if (strValue.length ===
|
|
743
|
+
if (strValue.length === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.ShortHexGuid)) {
|
|
777
744
|
// short hex guid
|
|
778
|
-
return
|
|
745
|
+
return Guid.shortGuidToFullGuid(strValue);
|
|
779
746
|
}
|
|
780
|
-
else if (strValue.length ===
|
|
747
|
+
else if (strValue.length === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.FullHexGuid)) {
|
|
781
748
|
// already a full hex guid
|
|
782
749
|
return strValue;
|
|
783
750
|
}
|
|
784
|
-
else if (strValue.length ===
|
|
751
|
+
else if (strValue.length === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.Base64Guid)) {
|
|
752
|
+
// base64 guid
|
|
785
753
|
// base64 guid
|
|
786
|
-
const
|
|
787
|
-
|
|
754
|
+
const binary = atob(strValue);
|
|
755
|
+
const bytes = new Uint8Array(binary.length);
|
|
756
|
+
for (let i = 0; i < binary.length; i++) {
|
|
757
|
+
bytes[i] = binary.charCodeAt(i);
|
|
758
|
+
}
|
|
759
|
+
const shortGuid = Array.from(bytes)
|
|
760
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
761
|
+
.join('');
|
|
762
|
+
return Guid.shortGuidToFullGuid(shortGuid);
|
|
788
763
|
}
|
|
789
764
|
else {
|
|
790
765
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
@@ -795,36 +770,39 @@ class GuidV4 {
|
|
|
795
770
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
796
771
|
}
|
|
797
772
|
if (typeof guid === 'bigint') {
|
|
798
|
-
const fullHex =
|
|
773
|
+
const fullHex = Guid.toFullHexFromBigInt(guid);
|
|
799
774
|
return fullHex.replace(/-/g, '');
|
|
800
775
|
}
|
|
801
|
-
else if (
|
|
802
|
-
guid.length ===
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
}
|
|
808
|
-
else {
|
|
809
|
-
return guid.toString('hex');
|
|
810
|
-
}
|
|
776
|
+
else if (Guid.isUint8Array(guid) &&
|
|
777
|
+
guid.length ===
|
|
778
|
+
Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer)) {
|
|
779
|
+
return Array.from(guid)
|
|
780
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
781
|
+
.join('');
|
|
811
782
|
}
|
|
812
|
-
else if (
|
|
783
|
+
else if (Guid.isUint8Array(guid)) {
|
|
813
784
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
814
785
|
}
|
|
815
786
|
// all remaining cases are string types
|
|
816
787
|
const strValue = String(guid);
|
|
817
|
-
if (strValue.length ===
|
|
788
|
+
if (strValue.length === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.ShortHexGuid)) {
|
|
818
789
|
// already a short hex guid
|
|
819
790
|
return strValue;
|
|
820
791
|
}
|
|
821
|
-
else if (strValue.length ===
|
|
792
|
+
else if (strValue.length === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.FullHexGuid)) {
|
|
822
793
|
// full hex guid
|
|
823
794
|
return strValue.replace(/-/g, '');
|
|
824
795
|
}
|
|
825
|
-
else if (strValue.length ===
|
|
796
|
+
else if (strValue.length === Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.Base64Guid)) {
|
|
826
797
|
// base64 guid
|
|
827
|
-
|
|
798
|
+
const binary = atob(strValue);
|
|
799
|
+
const bytes = new Uint8Array(binary.length);
|
|
800
|
+
for (let i = 0; i < binary.length; i++) {
|
|
801
|
+
bytes[i] = binary.charCodeAt(i);
|
|
802
|
+
}
|
|
803
|
+
return Array.from(bytes)
|
|
804
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
805
|
+
.join('');
|
|
828
806
|
}
|
|
829
807
|
else {
|
|
830
808
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
@@ -836,7 +814,7 @@ class GuidV4 {
|
|
|
836
814
|
* @returns The bigint as a full hex GUID.
|
|
837
815
|
*/
|
|
838
816
|
static toFullHexFromBigInt(bigInt) {
|
|
839
|
-
if (bigInt < 0n || bigInt >
|
|
817
|
+
if (bigInt < 0n || bigInt > Guid.MAX_BIGINT_VALUE) {
|
|
840
818
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
841
819
|
}
|
|
842
820
|
const uuidBigInt = bigInt.toString(16).padStart(32, '0');
|
|
@@ -856,66 +834,89 @@ class GuidV4 {
|
|
|
856
834
|
return rebuiltUuid;
|
|
857
835
|
}
|
|
858
836
|
/**
|
|
859
|
-
* Converts a given GUID value to a raw GUID
|
|
837
|
+
* Converts a given GUID value to a raw GUID array.
|
|
860
838
|
* @param value The GUID value to convert.
|
|
861
|
-
* @returns The GUID value as a raw GUID
|
|
839
|
+
* @returns The GUID value as a raw GUID array.
|
|
862
840
|
*/
|
|
863
|
-
static
|
|
864
|
-
const expectedBrand =
|
|
865
|
-
let rawGuidBufferResult =
|
|
841
|
+
static toRawGuidPlatformBuffer(value) {
|
|
842
|
+
const expectedBrand = Guid.whichBrand(value);
|
|
843
|
+
let rawGuidBufferResult = new Uint8Array(0);
|
|
866
844
|
switch (expectedBrand) {
|
|
867
|
-
case guid_brand_type_1.GuidBrandType.FullHexGuid:
|
|
868
|
-
|
|
845
|
+
case guid_brand_type_1.GuidBrandType.FullHexGuid: {
|
|
846
|
+
const hex1 = Guid.toShortHexGuid(value);
|
|
847
|
+
rawGuidBufferResult = new Uint8Array(hex1.length / 2);
|
|
848
|
+
for (let i = 0; i < hex1.length; i += 2) {
|
|
849
|
+
rawGuidBufferResult[i / 2] = parseInt(hex1.slice(i, i + 2), 16);
|
|
850
|
+
}
|
|
869
851
|
break;
|
|
870
|
-
|
|
871
|
-
|
|
852
|
+
}
|
|
853
|
+
case guid_brand_type_1.GuidBrandType.ShortHexGuid: {
|
|
854
|
+
const hex2 = Guid.toShortHexGuid(value);
|
|
855
|
+
rawGuidBufferResult = new Uint8Array(hex2.length / 2);
|
|
856
|
+
for (let i = 0; i < hex2.length; i += 2) {
|
|
857
|
+
rawGuidBufferResult[i / 2] = parseInt(hex2.slice(i, i + 2), 16);
|
|
858
|
+
}
|
|
872
859
|
break;
|
|
860
|
+
}
|
|
873
861
|
case guid_brand_type_1.GuidBrandType.Base64Guid:
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
862
|
+
if (typeof value === 'string' || Guid.isUint8Array(value)) {
|
|
863
|
+
const b64 = value.toString();
|
|
864
|
+
const binary = atob(b64);
|
|
865
|
+
rawGuidBufferResult = new Uint8Array(binary.length);
|
|
866
|
+
for (let i = 0; i < binary.length; i++) {
|
|
867
|
+
rawGuidBufferResult[i] = binary.charCodeAt(i);
|
|
868
|
+
}
|
|
877
869
|
}
|
|
878
870
|
else {
|
|
879
871
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
880
872
|
}
|
|
881
873
|
break;
|
|
882
|
-
case guid_brand_type_1.GuidBrandType.
|
|
874
|
+
case guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer:
|
|
883
875
|
rawGuidBufferResult = value;
|
|
884
876
|
break;
|
|
885
|
-
case guid_brand_type_1.GuidBrandType.BigIntGuid:
|
|
886
|
-
|
|
877
|
+
case guid_brand_type_1.GuidBrandType.BigIntGuid: {
|
|
878
|
+
const hex3 = Guid.toShortHexGuid(Guid.toFullHexFromBigInt(value));
|
|
879
|
+
rawGuidBufferResult = new Uint8Array(hex3.length / 2);
|
|
880
|
+
for (let i = 0; i < hex3.length; i += 2) {
|
|
881
|
+
rawGuidBufferResult[i / 2] = parseInt(hex3.slice(i, i + 2), 16);
|
|
882
|
+
}
|
|
887
883
|
break;
|
|
884
|
+
}
|
|
888
885
|
default:
|
|
889
886
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownBrand);
|
|
890
887
|
}
|
|
891
888
|
if (rawGuidBufferResult.length !==
|
|
892
|
-
|
|
889
|
+
Guid.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidPlatformBuffer)) {
|
|
893
890
|
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuidUnknownLength, undefined, rawGuidBufferResult.length);
|
|
894
891
|
}
|
|
895
892
|
return rawGuidBufferResult;
|
|
896
893
|
}
|
|
897
894
|
/**
|
|
898
|
-
* Compare two
|
|
899
|
-
* @param other - The other
|
|
895
|
+
* Compare two Guid instances for equality.
|
|
896
|
+
* @param other - The other Guid instance to compare (can be null/undefined)
|
|
900
897
|
* @param constantTime - Use constant-time comparison to prevent timing attacks (default: false)
|
|
901
|
-
* @returns True if the two
|
|
898
|
+
* @returns True if the two Guid instances are equal, false otherwise
|
|
902
899
|
*/
|
|
903
900
|
equals(other, constantTime = false) {
|
|
904
901
|
if (!other) {
|
|
905
902
|
return false;
|
|
906
903
|
}
|
|
907
904
|
if (constantTime) {
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
const a = this.asRawGuidBufferUnsafe;
|
|
911
|
-
const b = other.asRawGuidBuffer;
|
|
905
|
+
const a = this.asRawGuidPlatformBufferUnsafe;
|
|
906
|
+
const b = other.asRawGuidPlatformBuffer;
|
|
912
907
|
let result = 0;
|
|
913
908
|
for (let i = 0; i < 16; i++) {
|
|
914
909
|
result |= a[i] ^ b[i];
|
|
915
910
|
}
|
|
916
911
|
return result === 0;
|
|
917
912
|
}
|
|
918
|
-
|
|
913
|
+
const a = this.asRawGuidPlatformBufferUnsafe;
|
|
914
|
+
const b = other.asRawGuidPlatformBuffer;
|
|
915
|
+
for (let i = 0; i < 16; i++) {
|
|
916
|
+
if (a[i] !== b[i])
|
|
917
|
+
return false;
|
|
918
|
+
}
|
|
919
|
+
return true;
|
|
919
920
|
}
|
|
920
921
|
/**
|
|
921
922
|
* Checks if this GUID is empty (all zeros).
|
|
@@ -936,17 +937,17 @@ class GuidV4 {
|
|
|
936
937
|
* @returns True if the GUID is null, undefined, or empty
|
|
937
938
|
*/
|
|
938
939
|
static isNilOrEmpty(guid) {
|
|
939
|
-
return !guid || (guid instanceof
|
|
940
|
+
return !guid || (guid instanceof Guid && guid.isEmpty());
|
|
940
941
|
}
|
|
941
942
|
/**
|
|
942
|
-
* Creates a new
|
|
943
|
-
* @returns A new
|
|
943
|
+
* Creates a new Guid instance with the same value as this one.
|
|
944
|
+
* @returns A new Guid instance with identical value
|
|
944
945
|
*/
|
|
945
946
|
clone() {
|
|
946
|
-
return new
|
|
947
|
+
return new Guid(Uint8Array.from(this._value));
|
|
947
948
|
}
|
|
948
949
|
/**
|
|
949
|
-
* Returns the hash code for this GUID based on its
|
|
950
|
+
* Returns the hash code for this GUID based on its array content.
|
|
950
951
|
* Useful for using GUIDs as Map/Set keys.
|
|
951
952
|
* @returns A numeric hash code
|
|
952
953
|
*/
|
|
@@ -965,7 +966,7 @@ class GuidV4 {
|
|
|
965
966
|
*/
|
|
966
967
|
getVersion() {
|
|
967
968
|
// Skip boundary values
|
|
968
|
-
if (
|
|
969
|
+
if (Guid.isBoundaryValue(this.asFullHexGuid)) {
|
|
969
970
|
return undefined;
|
|
970
971
|
}
|
|
971
972
|
// Version is in bits 48-51 (byte 6, high nibble)
|
|
@@ -974,6 +975,13 @@ class GuidV4 {
|
|
|
974
975
|
// Valid RFC 4122 versions are 1-5
|
|
975
976
|
return version >= 1 && version <= 5 ? version : undefined;
|
|
976
977
|
}
|
|
978
|
+
/**
|
|
979
|
+
* Validates that this GUID is a proper v3 GUID according to RFC 4122.
|
|
980
|
+
* @returns True if valid v3 GUID, false otherwise
|
|
981
|
+
*/
|
|
982
|
+
isValidV3() {
|
|
983
|
+
return this.getVersion() === 3;
|
|
984
|
+
}
|
|
977
985
|
/**
|
|
978
986
|
* Validates that this GUID is a proper v4 GUID according to RFC 4122.
|
|
979
987
|
* Boundary values (all zeros/all Fs) return true as they're mathematically valid.
|
|
@@ -981,12 +989,27 @@ class GuidV4 {
|
|
|
981
989
|
*/
|
|
982
990
|
isValidV4() {
|
|
983
991
|
// Boundary values are considered valid
|
|
984
|
-
if (
|
|
992
|
+
if (Guid.isBoundaryValue(this.asFullHexGuid)) {
|
|
985
993
|
return true;
|
|
986
994
|
}
|
|
987
995
|
const version = this.getVersion();
|
|
988
996
|
return version === 4;
|
|
989
997
|
}
|
|
998
|
+
/**
|
|
999
|
+
* Validates that this GUID is a proper v5 GUID according to RFC 4122.
|
|
1000
|
+
* @returns True if valid v5 GUID, false otherwise
|
|
1001
|
+
*/
|
|
1002
|
+
isValidV5() {
|
|
1003
|
+
return this.getVersion() === 5;
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Returns a human-readable string representation.
|
|
1007
|
+
*/
|
|
1008
|
+
toDebugString() {
|
|
1009
|
+
const version = this.getVersion();
|
|
1010
|
+
const variant = this.getVariant();
|
|
1011
|
+
return `Guid(${this.asFullHexGuid}, v${version ?? '?'}, variant=${variant ?? '?'})`;
|
|
1012
|
+
}
|
|
990
1013
|
/**
|
|
991
1014
|
* Compares two GUIDs for ordering.
|
|
992
1015
|
* Useful for sorting GUID arrays.
|
|
@@ -994,8 +1017,86 @@ class GuidV4 {
|
|
|
994
1017
|
* @returns -1 if this < other, 0 if equal, 1 if this > other
|
|
995
1018
|
*/
|
|
996
1019
|
compareTo(other) {
|
|
997
|
-
|
|
1020
|
+
const a = this.asRawGuidPlatformBufferUnsafe;
|
|
1021
|
+
const b = other.asRawGuidPlatformBuffer;
|
|
1022
|
+
for (let i = 0; i < 16; i++) {
|
|
1023
|
+
if (a[i] < b[i])
|
|
1024
|
+
return -1;
|
|
1025
|
+
if (a[i] > b[i])
|
|
1026
|
+
return 1;
|
|
1027
|
+
}
|
|
1028
|
+
return 0;
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Returns the timestamp from a v1 GUID.
|
|
1032
|
+
* @returns Date object or undefined if not a v1 GUID
|
|
1033
|
+
*/
|
|
1034
|
+
getTimestamp() {
|
|
1035
|
+
if (this.getVersion() !== 1)
|
|
1036
|
+
return undefined;
|
|
1037
|
+
const bytes = this._value;
|
|
1038
|
+
const timeLow = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
|
|
1039
|
+
const timeMid = (bytes[4] << 8) | bytes[5];
|
|
1040
|
+
const timeHigh = ((bytes[6] & 0x0f) << 8) | bytes[7];
|
|
1041
|
+
const timestamp = (BigInt(timeHigh) << 48n) |
|
|
1042
|
+
(BigInt(timeMid) << 32n) |
|
|
1043
|
+
BigInt(timeLow >>> 0);
|
|
1044
|
+
const unixTimestamp = Number(timestamp - 122192928000000000n) / 10000;
|
|
1045
|
+
return new Date(unixTimestamp);
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Extracts the variant from the GUID.
|
|
1049
|
+
* @returns The variant (0-2) or undefined
|
|
1050
|
+
*/
|
|
1051
|
+
getVariant() {
|
|
1052
|
+
const variantByte = this._value[8];
|
|
1053
|
+
if ((variantByte & 0x80) === 0)
|
|
1054
|
+
return 0; // NCS
|
|
1055
|
+
if ((variantByte & 0xc0) === 0x80)
|
|
1056
|
+
return 1; // RFC 4122
|
|
1057
|
+
if ((variantByte & 0xe0) === 0xc0)
|
|
1058
|
+
return 2; // Microsoft
|
|
1059
|
+
return undefined;
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Creates a v1 GUID (time-based).
|
|
1063
|
+
* @returns A new Guid instance containing a v1 GUID
|
|
1064
|
+
*/
|
|
1065
|
+
static v1() {
|
|
1066
|
+
try {
|
|
1067
|
+
const v1Guid = uuid.v1();
|
|
1068
|
+
return new Guid(v1Guid);
|
|
1069
|
+
}
|
|
1070
|
+
catch (error) {
|
|
1071
|
+
if (error instanceof guid_1.GuidError)
|
|
1072
|
+
throw error;
|
|
1073
|
+
throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.InvalidGuid);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Validates that this GUID is a proper v1 GUID.
|
|
1078
|
+
* @returns True if valid v1 GUID, false otherwise
|
|
1079
|
+
*/
|
|
1080
|
+
isValidV1() {
|
|
1081
|
+
return this.getVersion() === 1;
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Returns a URL-safe base64 representation (no padding, URL-safe chars).
|
|
1085
|
+
*/
|
|
1086
|
+
get asUrlSafeBase64() {
|
|
1087
|
+
const base64 = btoa(String.fromCharCode(...this._value));
|
|
1088
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Creates a GUID from URL-safe base64.
|
|
1092
|
+
*/
|
|
1093
|
+
static fromUrlSafeBase64(urlSafe) {
|
|
1094
|
+
const base64 = urlSafe
|
|
1095
|
+
.replace(/-/g, '+')
|
|
1096
|
+
.replace(/_/g, '/')
|
|
1097
|
+
.padEnd(24, '=');
|
|
1098
|
+
return new Guid(base64);
|
|
998
1099
|
}
|
|
999
1100
|
}
|
|
1000
|
-
exports.
|
|
1101
|
+
exports.Guid = Guid;
|
|
1001
1102
|
//# sourceMappingURL=guid.js.map
|