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