@digitaldefiance/ecies-lib 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +8 -20
  2. package/package.json +2 -2
  3. package/src/constants.d.ts +1 -1
  4. package/src/constants.d.ts.map +1 -1
  5. package/src/constants.js +7 -10
  6. package/src/constants.js.map +1 -1
  7. package/src/core/errors/crypto-error.d.ts +1 -2
  8. package/src/core/errors/crypto-error.d.ts.map +1 -1
  9. package/src/core/errors/crypto-error.js +1 -3
  10. package/src/core/errors/crypto-error.js.map +1 -1
  11. package/src/enumerations/ecies-string-key.d.ts +0 -4
  12. package/src/enumerations/ecies-string-key.d.ts.map +1 -1
  13. package/src/enumerations/ecies-string-key.js +0 -5
  14. package/src/enumerations/ecies-string-key.js.map +1 -1
  15. package/src/enumerations/index.d.ts +0 -2
  16. package/src/enumerations/index.d.ts.map +1 -1
  17. package/src/enumerations/index.js +0 -2
  18. package/src/enumerations/index.js.map +1 -1
  19. package/src/errors/index.d.ts +0 -1
  20. package/src/errors/index.d.ts.map +1 -1
  21. package/src/errors/index.js +0 -1
  22. package/src/errors/index.js.map +1 -1
  23. package/src/i18n-setup.d.ts.map +1 -1
  24. package/src/i18n-setup.js +0 -35
  25. package/src/i18n-setup.js.map +1 -1
  26. package/src/index.d.ts +0 -2
  27. package/src/index.d.ts.map +1 -1
  28. package/src/index.js +1 -4
  29. package/src/index.js.map +1 -1
  30. package/src/interfaces/constants.d.ts +0 -1
  31. package/src/interfaces/constants.d.ts.map +1 -1
  32. package/src/interfaces/index.d.ts +0 -1
  33. package/src/interfaces/index.d.ts.map +1 -1
  34. package/src/interfaces/index.js +0 -1
  35. package/src/interfaces/index.js.map +1 -1
  36. package/src/interfaces/member-operational.d.ts +2 -2
  37. package/src/interfaces/member-operational.d.ts.map +1 -1
  38. package/src/member.d.ts +5 -5
  39. package/src/member.d.ts.map +1 -1
  40. package/src/member.js +6 -6
  41. package/src/member.js.map +1 -1
  42. package/src/secure-buffer.d.ts +5 -6
  43. package/src/secure-buffer.d.ts.map +1 -1
  44. package/src/secure-buffer.js +7 -7
  45. package/src/secure-buffer.js.map +1 -1
  46. package/src/secure-string.d.ts +2 -3
  47. package/src/secure-string.d.ts.map +1 -1
  48. package/src/secure-string.js +4 -4
  49. package/src/secure-string.js.map +1 -1
  50. package/src/types.d.ts +0 -33
  51. package/src/types.d.ts.map +1 -1
  52. package/src/enumerations/guid-brand-type.d.ts +0 -31
  53. package/src/enumerations/guid-brand-type.d.ts.map +0 -1
  54. package/src/enumerations/guid-brand-type.js +0 -35
  55. package/src/enumerations/guid-brand-type.js.map +0 -1
  56. package/src/enumerations/guid-error-type.d.ts +0 -7
  57. package/src/enumerations/guid-error-type.d.ts.map +0 -1
  58. package/src/enumerations/guid-error-type.js +0 -11
  59. package/src/enumerations/guid-error-type.js.map +0 -1
  60. package/src/errors/guid.d.ts +0 -15
  61. package/src/errors/guid.d.ts.map +0 -1
  62. package/src/errors/guid.js +0 -28
  63. package/src/errors/guid.js.map +0 -1
  64. package/src/guid.d.ts +0 -168
  65. package/src/guid.d.ts.map +0 -1
  66. package/src/guid.js +0 -720
  67. package/src/guid.js.map +0 -1
  68. package/src/interfaces/guid.d.ts +0 -45
  69. package/src/interfaces/guid.d.ts.map +0 -1
  70. package/src/interfaces/guid.js +0 -3
  71. package/src/interfaces/guid.js.map +0 -1
package/src/guid.js DELETED
@@ -1,720 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GuidV4 = void 0;
4
- const tslib_1 = require("tslib");
5
- const uuid = tslib_1.__importStar(require("uuid"));
6
- const guid_brand_type_1 = require("./enumerations/guid-brand-type");
7
- const guid_error_type_1 = require("./enumerations/guid-error-type");
8
- const guid_1 = require("./errors/guid");
9
- const utils_1 = require("./utils");
10
- const bson_1 = require("bson");
11
- /**
12
- * GuidV4 represents a GUID (Globally Unique Identifier) that is compliant with the RFC 4122 standard.
13
- * GuidV4 instances can be created from a variety of input types, including:
14
- * - FullHexGuid: A 36-character string representation of the GUID, including dashes
15
- * - ShortHexGuid: A 32-character string representation of the GUID, excluding dashes
16
- * - Base64Guid: A 24-character base64-encoded string representation of the GUID
17
- * - BigIntGuid: A bigint representation of the GUID
18
- * - RawGuidUint8Array: A 16-byte Uint8Array representation of the GUID
19
- * GuidV4 instances can be converted to any of these representations using the appropriate method.
20
- */
21
- class GuidV4 {
22
- /**
23
- * GUID is stored internally as a raw 16-byte Uint8Array.
24
- */
25
- _value;
26
- constructor(value) {
27
- // Check for UnknownLength issues first - this applies to any input type
28
- let brandCheckPassed = false;
29
- try {
30
- GuidV4.whichBrand(value);
31
- brandCheckPassed = true;
32
- }
33
- catch (error) {
34
- if (error instanceof guid_1.GuidError &&
35
- (error.type === guid_error_type_1.GuidErrorType.UnknownLength ||
36
- error.type === guid_error_type_1.GuidErrorType.UnknownBrand)) {
37
- // For Uint8Array with wrong length, treat as Invalid rather than UnknownLength
38
- if (value instanceof Uint8Array &&
39
- error.type === guid_error_type_1.GuidErrorType.UnknownLength) {
40
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
41
- }
42
- throw error; // Let UnknownLength/UnknownBrand bubble up for other types
43
- }
44
- // For other errors from whichBrand, continue with validation
45
- }
46
- // Only do validation if brand check passed
47
- if (brandCheckPassed) {
48
- try {
49
- if (!GuidV4.isValid(value)) {
50
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
51
- }
52
- }
53
- catch (error) {
54
- if (error instanceof guid_1.GuidError) {
55
- throw error;
56
- }
57
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
58
- }
59
- }
60
- this._value = GuidV4.toRawGuidUint8Array(value);
61
- }
62
- static isValid(value) {
63
- try {
64
- if (value === null || value === undefined) {
65
- return false;
66
- }
67
- const strValue = String(value);
68
- if (!strValue && value !== 0n) {
69
- return false;
70
- }
71
- const expectedBrand = GuidV4.whichBrand(value);
72
- const verifiedBrand = GuidV4.verifyGuid(expectedBrand, value);
73
- if (!verifiedBrand) {
74
- return false;
75
- }
76
- // Skip UUID validation for MongoObjectId - they have different structure
77
- if (expectedBrand === guid_brand_type_1.GuidBrandType.MongoObjectId) {
78
- return true;
79
- }
80
- const uintArray = GuidV4.toRawGuidUint8Array(value);
81
- // Skip UUID validation for boundary values that are mathematically valid but not RFC 4122 compliant
82
- const fullHex = GuidV4.toFullHexGuid(uintArray);
83
- const isAllZeros = fullHex === '00000000-0000-0000-0000-000000000000';
84
- const isAllFs = fullHex === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
85
- if (!isAllZeros && !isAllFs && !uuid.validate(fullHex)) {
86
- return false;
87
- }
88
- return true;
89
- }
90
- catch (error) {
91
- return false;
92
- }
93
- }
94
- static validateUuid(value) {
95
- return uuid.validate(value);
96
- }
97
- serialize() {
98
- return this.asBase64Guid;
99
- }
100
- static hydrate(value) {
101
- return new GuidV4(value);
102
- }
103
- static LengthMap = {
104
- [guid_brand_type_1.GuidBrandType.Unknown]: -1,
105
- [guid_brand_type_1.GuidBrandType.FullHexGuid]: 36,
106
- [guid_brand_type_1.GuidBrandType.ShortHexGuid]: 32,
107
- [guid_brand_type_1.GuidBrandType.Base64Guid]: 24,
108
- [guid_brand_type_1.GuidBrandType.RawGuidUint8Array]: 16,
109
- [guid_brand_type_1.GuidBrandType.MongoObjectId]: 24, // 24-char hex string
110
- [guid_brand_type_1.GuidBrandType.BigIntGuid]: -1, // Variable length
111
- };
112
- static ReverseLengthMap = {
113
- 0: guid_brand_type_1.GuidBrandType.Unknown,
114
- 36: guid_brand_type_1.GuidBrandType.FullHexGuid,
115
- 32: guid_brand_type_1.GuidBrandType.ShortHexGuid,
116
- 24: guid_brand_type_1.GuidBrandType.Base64Guid, // Note: MongoObjectId is also 24 chars, handled in whichBrand
117
- 16: guid_brand_type_1.GuidBrandType.RawGuidUint8Array,
118
- // BigIntGuid is variable length, so it is not included in the reverse map
119
- };
120
- static VerifyFunctions = {
121
- [guid_brand_type_1.GuidBrandType.Unknown]: () => false,
122
- [guid_brand_type_1.GuidBrandType.FullHexGuid]: (guid) => this.isFullHexGuid(guid),
123
- [guid_brand_type_1.GuidBrandType.ShortHexGuid]: (guid) => this.isShortHexGuid(guid),
124
- [guid_brand_type_1.GuidBrandType.Base64Guid]: (guid) => this.isBase64Guid(guid),
125
- [guid_brand_type_1.GuidBrandType.BigIntGuid]: (guid) => this.isBigIntGuid(guid),
126
- [guid_brand_type_1.GuidBrandType.MongoObjectId]: (guid) => this.isMongoObjectIdGuid(guid),
127
- [guid_brand_type_1.GuidBrandType.RawGuidUint8Array]: (guid) => this.isRawGuidUint8Array(guid),
128
- };
129
- /**
130
- * Returns the GUID as a raw Uint8Array.
131
- */
132
- get asRawGuidUint8Array() {
133
- return this._value;
134
- }
135
- static new() {
136
- try {
137
- const uuidStr = uuid.v4();
138
- if (!uuidStr) {
139
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
140
- }
141
- return new GuidV4(uuidStr);
142
- }
143
- catch (error) {
144
- // If there's an error creating the GUID, throw a more specific error
145
- if (error instanceof guid_1.GuidError) {
146
- throw error;
147
- }
148
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
149
- }
150
- }
151
- /**
152
- * Creates a GuidV4 from a MongoDB ObjectId (BSON ObjectId or hex string)
153
- * Pads the ObjectId to 32 characters to create a valid GUID
154
- * @param objectId - BSON ObjectId instance or 24-character hex string
155
- * @returns GuidV4 instance
156
- */
157
- static fromMongoObjectId(objectId) {
158
- let hexString;
159
- if (objectId instanceof bson_1.ObjectId) {
160
- hexString = objectId.toHexString();
161
- }
162
- else if (typeof objectId === 'string') {
163
- hexString = objectId;
164
- }
165
- else {
166
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
167
- }
168
- const cleaned = hexString.toLowerCase().trim();
169
- if (cleaned.length !== 24 || !/^[0-9a-f]{24}$/.test(cleaned)) {
170
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
171
- }
172
- const paddedHex = cleaned.padEnd(32, '0');
173
- return new GuidV4(paddedHex);
174
- }
175
- /**
176
- * Returns the GUID as a full hex string.
177
- */
178
- get asFullHexGuid() {
179
- return GuidV4.toFullHexGuid(this._value);
180
- }
181
- /**
182
- * Returns the GUID as a raw Uint8Array.
183
- */
184
- get asUint8Array() {
185
- return this._value;
186
- }
187
- /**
188
- * Returns the GUID as a short hex string.
189
- */
190
- get asShortHexGuid() {
191
- return GuidV4.toShortHexGuid(this.asFullHexGuid);
192
- }
193
- /**
194
- * Returns the GUID as a base64 string.
195
- */
196
- toString() {
197
- return this.asBase64Guid;
198
- }
199
- /**
200
- * Returns the GUID as a JSON string.
201
- * @returns The GUID as a JSON string.
202
- */
203
- toJson() {
204
- return JSON.stringify(this.asBase64Guid);
205
- }
206
- /**
207
- * Returns the GUID as a bigint.
208
- */
209
- get asBigIntGuid() {
210
- const hex = (0, utils_1.uint8ArrayToHex)(this._value);
211
- return BigInt('0x' + hex);
212
- }
213
- /**
214
- * Returns the GUID as a base64 string.
215
- */
216
- get asBase64Guid() {
217
- return btoa(String.fromCharCode(...this._value));
218
- }
219
- /**
220
- * Converts the GUID back to a MongoDB ObjectId hex string (24 characters)
221
- * Strips the padding zeros that were added during conversion
222
- * @returns 24-character hex string compatible with MongoDB ObjectId
223
- */
224
- get asMongoObjectId() {
225
- const shortHex = this.asShortHexGuid;
226
- // Remove trailing zeros (padding) to get back to 24 chars
227
- const objectIdHex = shortHex.slice(0, 24);
228
- return objectIdHex;
229
- }
230
- /**
231
- * Verifies if a given GUID is valid for the given brand.
232
- * @param guidBrand The brand of the GUID to verify.
233
- * @param guid The GUID to verify.
234
- * @returns True if the GUID is valid for the given brand, false otherwise.
235
- */
236
- static verifyGuid(guidBrand, guid) {
237
- if (guid === null || guid === undefined) {
238
- return false;
239
- }
240
- try {
241
- const verifyFunc = GuidV4.VerifyFunctions[guidBrand];
242
- return verifyFunc(guid);
243
- }
244
- catch {
245
- return false;
246
- }
247
- }
248
- /**
249
- * Returns the length of the GUID for the given brand.
250
- * @param guidBrand The brand of the GUID to get the length for.
251
- * @returns The length of the GUID for the given brand.
252
- */
253
- static guidBrandToLength(guidBrand) {
254
- const length = GuidV4.LengthMap[guidBrand];
255
- if (length <= 0) {
256
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.UnknownBrand, guidBrand);
257
- }
258
- return length;
259
- }
260
- /**
261
- * Returns the brand of the GUID for the given length.
262
- * @param length The length of the GUID to get the brand for.
263
- * @param isUint8Array Whether the GUID is a Uint8Array.
264
- * @returns The brand of the GUID for the given length.
265
- */
266
- static lengthToGuidBrand(length, isUint8Array) {
267
- if (length <= 0) {
268
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.UnknownLength, undefined, length);
269
- }
270
- const keys = Object.keys(GuidV4.ReverseLengthMap);
271
- const values = Object.values(GuidV4.ReverseLengthMap);
272
- for (let i = 0; i < keys.length; i++) {
273
- const len = parseInt(keys[i]);
274
- const brand = values[i];
275
- if (len === length) {
276
- if (isUint8Array && !brand.endsWith('Uint8Array')) {
277
- continue;
278
- }
279
- else if (!isUint8Array && brand.endsWith('Uint8Array')) {
280
- continue;
281
- }
282
- return brand;
283
- }
284
- }
285
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.UnknownLength, undefined, length);
286
- }
287
- /**
288
- * Verifies if a given GUID is a valid full hex GUID.
289
- * @param fullHexGuidValue The full hex GUID to verify.
290
- * @returns True if the GUID is a valid full hex GUID, false otherwise.
291
- */
292
- static isFullHexGuid(fullHexGuidValue) {
293
- try {
294
- if (fullHexGuidValue === null || fullHexGuidValue === undefined) {
295
- return false;
296
- }
297
- const expectedLength = GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.FullHexGuid);
298
- const strValue = String(fullHexGuidValue);
299
- if (strValue.length !== expectedLength) {
300
- return false;
301
- }
302
- // Allow boundary values (all zeros and all fs)
303
- const isAllZeros = strValue === '00000000-0000-0000-0000-000000000000';
304
- const isAllFs = strValue === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
305
- return isAllZeros || isAllFs || GuidV4.validateUuid(strValue);
306
- }
307
- catch {
308
- return false;
309
- }
310
- }
311
- /**
312
- * Verifies if a given GUID is a valid short hex GUID.
313
- * @param shortHexGuidValue The short hex GUID to verify.
314
- * @returns True if the GUID is a valid short hex GUID, false otherwise.
315
- */
316
- static isShortHexGuid(shortHexGuidValue) {
317
- try {
318
- if (shortHexGuidValue === null || shortHexGuidValue === undefined) {
319
- return false;
320
- }
321
- const expectedLength = GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.ShortHexGuid);
322
- const strValue = String(shortHexGuidValue);
323
- if (strValue.length !== expectedLength) {
324
- return false;
325
- }
326
- try {
327
- const fullHexGuid = GuidV4.toFullHexGuid(strValue);
328
- // Allow boundary values (all zeros and all fs)
329
- const isAllZeros = fullHexGuid === '00000000-0000-0000-0000-000000000000';
330
- const isAllFs = fullHexGuid === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
331
- return isAllZeros || isAllFs || uuid.validate(fullHexGuid);
332
- }
333
- catch {
334
- return false;
335
- }
336
- }
337
- catch {
338
- return false;
339
- }
340
- }
341
- /**
342
- * Verifies if a given GUID is a valid base64 GUID.
343
- * @param value The base64 GUID to verify.
344
- * @returns True if the GUID is a valid base64 GUID, false otherwise.
345
- */
346
- static isBase64Guid(value) {
347
- try {
348
- if (value === null || value === undefined) {
349
- return false;
350
- }
351
- let valueLength;
352
- if (typeof value === 'bigint') {
353
- valueLength = value.toString(16).length;
354
- }
355
- else if (value instanceof Uint8Array) {
356
- valueLength = value.length;
357
- }
358
- else {
359
- valueLength = String(value).length;
360
- }
361
- const result = valueLength === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.Base64Guid);
362
- if (result) {
363
- try {
364
- const fromBase64 = GuidV4.toRawGuidUint8Array(value);
365
- const hex = (0, utils_1.uint8ArrayToHex)(fromBase64);
366
- const fullHexGuid = GuidV4.toFullHexGuid(hex);
367
- // Allow boundary values (all zeros and all fs)
368
- const isAllZeros = fullHexGuid === '00000000-0000-0000-0000-000000000000';
369
- const isAllFs = fullHexGuid === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
370
- return isAllZeros || isAllFs || uuid.validate(fullHexGuid);
371
- }
372
- catch {
373
- return false;
374
- }
375
- }
376
- return result;
377
- }
378
- catch {
379
- return false;
380
- }
381
- }
382
- /**
383
- * Verifies if a given GUID is a valid raw GUID buffer.
384
- * @param value The raw GUID buffer to verify.
385
- * @returns True if the GUID is a valid raw GUID buffer, false otherwise.
386
- */
387
- static isRawGuidUint8Array(value) {
388
- try {
389
- if (value === null || value === undefined) {
390
- return false;
391
- }
392
- const expectedLength = GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidUint8Array);
393
- let valueLength;
394
- if (typeof value === 'bigint') {
395
- valueLength = value.toString(16).length;
396
- }
397
- else if (value instanceof Uint8Array) {
398
- valueLength = value.length;
399
- }
400
- else {
401
- valueLength = String(value).length;
402
- }
403
- if (valueLength !== expectedLength) {
404
- return false;
405
- }
406
- try {
407
- if (!(value instanceof Uint8Array)) {
408
- return false;
409
- }
410
- const hex = (0, utils_1.uint8ArrayToHex)(value);
411
- const fullHexGuid = GuidV4.toFullHexGuid(hex);
412
- // Allow boundary values (all zeros and all fs)
413
- const isAllZeros = fullHexGuid === '00000000-0000-0000-0000-000000000000';
414
- const isAllFs = fullHexGuid === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
415
- return isAllZeros || isAllFs || GuidV4.validateUuid(fullHexGuid);
416
- }
417
- catch {
418
- return false;
419
- }
420
- }
421
- catch {
422
- return false;
423
- }
424
- }
425
- /**
426
- * Verifies if a given GUID is a valid bigint GUID.
427
- * @param value The bigint GUID to verify.
428
- * @returns True if the GUID is a valid bigint GUID, false otherwise.
429
- */
430
- static isBigIntGuid(value) {
431
- try {
432
- if (value === null || value === undefined) {
433
- return false;
434
- }
435
- if (typeof value !== 'bigint') {
436
- return false;
437
- }
438
- if (value < 0n) {
439
- return false;
440
- }
441
- const bigIntString = value.toString(16);
442
- if (bigIntString.length > 32) {
443
- return false;
444
- }
445
- try {
446
- const fromBigInt = GuidV4.toFullHexFromBigInt(value);
447
- // Allow boundary values (all zeros and all fs)
448
- const isAllZeros = fromBigInt === '00000000-0000-0000-0000-000000000000';
449
- const isAllFs = fromBigInt === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
450
- return isAllZeros || isAllFs || uuid.validate(fromBigInt);
451
- }
452
- catch {
453
- return false;
454
- }
455
- }
456
- catch {
457
- return false;
458
- }
459
- }
460
- static isMongoObjectIdGuid(value) {
461
- try {
462
- if (value === null || value === undefined) {
463
- return false;
464
- }
465
- // Accept BSON ObjectId instances
466
- if (value instanceof bson_1.ObjectId) {
467
- return true;
468
- }
469
- if (typeof value !== 'string') {
470
- return false;
471
- }
472
- const strValue = String(value);
473
- if (strValue.length !== 24) {
474
- return false;
475
- }
476
- if (!/^[0-9a-f]{24}$/i.test(strValue)) {
477
- return false;
478
- }
479
- return true;
480
- }
481
- catch {
482
- return false;
483
- }
484
- }
485
- /**
486
- * Determines the brand of a given GUID value.
487
- * @param value The GUID value to determine the brand of.
488
- * @returns The brand of the GUID value.
489
- */
490
- static whichBrand(value) {
491
- if (value === null || value === undefined) {
492
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
493
- }
494
- if (typeof value === 'bigint') {
495
- return guid_brand_type_1.GuidBrandType.BigIntGuid;
496
- }
497
- // Handle BSON ObjectId
498
- if (value instanceof bson_1.ObjectId) {
499
- return guid_brand_type_1.GuidBrandType.MongoObjectId;
500
- }
501
- const isUint8Array = value instanceof Uint8Array;
502
- // Special handling for 24-char strings: could be Base64 or MongoObjectId
503
- if (!isUint8Array && typeof value === 'string' && value.length === 24) {
504
- // Check if it's a valid hex string (MongoObjectId)
505
- if (/^[0-9a-f]{24}$/i.test(value)) {
506
- return guid_brand_type_1.GuidBrandType.MongoObjectId;
507
- }
508
- // Otherwise assume Base64
509
- return guid_brand_type_1.GuidBrandType.Base64Guid;
510
- }
511
- let expectedLength;
512
- if (isUint8Array) {
513
- expectedLength = value.length;
514
- }
515
- else {
516
- expectedLength = String(value).length;
517
- }
518
- return GuidV4.lengthToGuidBrand(expectedLength, isUint8Array);
519
- }
520
- /**
521
- * Converts a given short hex GUID to a full hex GUID.
522
- * @param shortGuid The short hex GUID to convert.
523
- * @returns The short hex GUID as a full hex GUID.
524
- */
525
- static shortGuidToFullGuid(shortGuid) {
526
- // insert dashes
527
- const str = shortGuid.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
528
- return str;
529
- }
530
- /**
531
- * Converts a given GUID value to a full hex GUID.
532
- * @param guid The GUID value to convert.
533
- * @param dateCreated The date created for Mongoose IDs
534
- * @returns The GUID value as a full hex GUID.
535
- */
536
- static toFullHexGuid(guid) {
537
- if (!guid) {
538
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
539
- }
540
- else if (typeof guid === 'bigint') {
541
- return GuidV4.toFullHexFromBigInt(guid);
542
- }
543
- else if (guid instanceof bson_1.ObjectId) {
544
- // Handle BSON ObjectId
545
- const hexStr = guid.toHexString();
546
- const paddedHex = hexStr.toLowerCase().padEnd(32, '0');
547
- return GuidV4.shortGuidToFullGuid(paddedHex);
548
- }
549
- else if (guid instanceof Uint8Array &&
550
- guid.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidUint8Array)) {
551
- const shortHex = (0, utils_1.uint8ArrayToHex)(guid);
552
- return GuidV4.shortGuidToFullGuid(shortHex);
553
- }
554
- else if (guid instanceof Uint8Array) {
555
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
556
- }
557
- // all remaining cases are string types
558
- const strValue = String(guid);
559
- if (strValue.length === 24 && /^[0-9a-f]{24}$/i.test(strValue)) {
560
- // MongoDB ObjectId - pad to 32 chars
561
- const paddedHex = strValue.toLowerCase().padEnd(32, '0');
562
- return GuidV4.shortGuidToFullGuid(paddedHex);
563
- }
564
- else if (strValue.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.ShortHexGuid)) {
565
- // short hex guid
566
- return GuidV4.shortGuidToFullGuid(strValue);
567
- }
568
- else if (strValue.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.FullHexGuid)) {
569
- // already a full hex guid
570
- return strValue;
571
- }
572
- else if (strValue.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.Base64Guid)) {
573
- // base64 guid
574
- const shortGuid = (0, utils_1.uint8ArrayToHex)((0, utils_1.base64ToUint8Array)(strValue));
575
- return GuidV4.shortGuidToFullGuid(shortGuid);
576
- }
577
- else {
578
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
579
- }
580
- }
581
- static toShortHexGuid(guid) {
582
- if (!guid) {
583
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
584
- }
585
- else if (typeof guid === 'bigint') {
586
- const fullHex = GuidV4.toFullHexFromBigInt(guid);
587
- return fullHex.replace(/-/g, '');
588
- }
589
- else if (guid instanceof bson_1.ObjectId) {
590
- // Handle BSON ObjectId
591
- const hexStr = guid.toHexString();
592
- return hexStr.toLowerCase().padEnd(32, '0');
593
- }
594
- else if (guid instanceof Uint8Array &&
595
- guid.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidUint8Array)) {
596
- return (0, utils_1.uint8ArrayToHex)(guid);
597
- }
598
- else if (guid instanceof Uint8Array) {
599
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
600
- }
601
- // all remaining cases are string types
602
- const strValue = String(guid);
603
- if (strValue.length === 24 && /^[0-9a-f]{24}$/i.test(strValue)) {
604
- // MongoDB ObjectId - pad to 32 chars
605
- return strValue.toLowerCase().padEnd(32, '0');
606
- }
607
- else if (strValue.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.ShortHexGuid)) {
608
- // already a short hex guid
609
- return strValue;
610
- }
611
- else if (strValue.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.FullHexGuid)) {
612
- // full hex guid
613
- return strValue.replace(/-/g, '');
614
- }
615
- else if (strValue.length === GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.Base64Guid)) {
616
- // base64 guid
617
- return (0, utils_1.uint8ArrayToHex)((0, utils_1.base64ToUint8Array)(strValue));
618
- }
619
- else {
620
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
621
- }
622
- }
623
- /**
624
- * Converts a given bigint to a full hex GUID.
625
- * @param bigInt The bigint to convert.
626
- * @returns The bigint as a full hex GUID.
627
- */
628
- static toFullHexFromBigInt(bigInt) {
629
- if (bigInt < 0n) {
630
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
631
- }
632
- const uuidBigInt = bigInt.toString(16).padStart(32, '0');
633
- if (uuidBigInt.length !== 32) {
634
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
635
- }
636
- const rebuiltUuid = uuidBigInt.slice(0, 8) +
637
- '-' +
638
- uuidBigInt.slice(8, 12) +
639
- '-' +
640
- uuidBigInt.slice(12, 16) +
641
- '-' +
642
- uuidBigInt.slice(16, 20) +
643
- '-' +
644
- uuidBigInt.slice(20);
645
- return rebuiltUuid;
646
- }
647
- /**
648
- * Converts a given GUID value to a raw GUID buffer.
649
- * @param value The GUID value to convert.
650
- * @param dateCreated The date the GUID was created for Mongoose GUIDs
651
- * @returns The GUID value as a raw GUID buffer.
652
- */
653
- static toRawGuidUint8Array(value) {
654
- const expectedBrand = GuidV4.whichBrand(value);
655
- let rawGuidUint8ArrayResult = new Uint8Array(0);
656
- switch (expectedBrand) {
657
- case guid_brand_type_1.GuidBrandType.FullHexGuid:
658
- const shortHex1 = GuidV4.toShortHexGuid(value);
659
- rawGuidUint8ArrayResult = new Uint8Array(shortHex1.match(/.{2}/g).map((byte) => parseInt(byte, 16)));
660
- break;
661
- case guid_brand_type_1.GuidBrandType.ShortHexGuid:
662
- const shortHex2 = GuidV4.toShortHexGuid(value);
663
- rawGuidUint8ArrayResult = new Uint8Array(shortHex2.match(/.{2}/g).map((byte) => parseInt(byte, 16)));
664
- break;
665
- case guid_brand_type_1.GuidBrandType.Base64Guid:
666
- // Ensure value is a string before using it with atob
667
- if (typeof value === 'string' || value instanceof Uint8Array) {
668
- const binaryString = atob(value.toString());
669
- rawGuidUint8ArrayResult = new Uint8Array(Array.from(binaryString).map((c) => c.charCodeAt(0)));
670
- }
671
- else {
672
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.Invalid);
673
- }
674
- break;
675
- case guid_brand_type_1.GuidBrandType.RawGuidUint8Array:
676
- rawGuidUint8ArrayResult = value;
677
- break;
678
- case guid_brand_type_1.GuidBrandType.BigIntGuid:
679
- const shortHex3 = GuidV4.toShortHexGuid(GuidV4.toFullHexFromBigInt(value));
680
- rawGuidUint8ArrayResult = new Uint8Array(shortHex3.match(/.{2}/g).map((byte) => parseInt(byte, 16)));
681
- break;
682
- case guid_brand_type_1.GuidBrandType.MongoObjectId:
683
- let hexStr;
684
- if (value instanceof bson_1.ObjectId) {
685
- hexStr = value.toHexString();
686
- }
687
- else {
688
- hexStr = String(value);
689
- }
690
- const paddedHex = hexStr.toLowerCase().padEnd(32, '0');
691
- rawGuidUint8ArrayResult = new Uint8Array(paddedHex.match(/.{2}/g).map((byte) => parseInt(byte, 16)));
692
- break;
693
- default:
694
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.UnknownBrand);
695
- }
696
- if (rawGuidUint8ArrayResult.length !==
697
- GuidV4.guidBrandToLength(guid_brand_type_1.GuidBrandType.RawGuidUint8Array)) {
698
- throw new guid_1.GuidError(guid_error_type_1.GuidErrorType.UnknownLength, undefined, rawGuidUint8ArrayResult.length);
699
- }
700
- return rawGuidUint8ArrayResult;
701
- }
702
- /**
703
- * Compare two GuidV4 instances
704
- * @param other - The other GuidV4 instance to compare
705
- * @returns True if the two GuidV4 instances are equal, false otherwise
706
- */
707
- equals(other) {
708
- const a = this.asRawGuidUint8Array;
709
- const b = other.asRawGuidUint8Array;
710
- if (a.length !== b.length)
711
- return false;
712
- for (let i = 0; i < a.length; i++) {
713
- if (a[i] !== b[i])
714
- return false;
715
- }
716
- return true;
717
- }
718
- }
719
- exports.GuidV4 = GuidV4;
720
- //# sourceMappingURL=guid.js.map