-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kibisis): remove web crypto api dependency when generating uuid (#…
…134) * fix(kibisis): remove web crypto api dependency when generating uuid * chore: squash * chore: squash * chore: squash * chore: squash
- Loading branch information
1 parent
85f8bef
commit 54b43e8
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { generateUuid } from './utils' | ||
import { randomUUID, getRandomValues } from 'crypto' | ||
|
||
describe(`${__dirname}/utils`, () => { | ||
const validUuidRegex = | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i | ||
|
||
describe('generateUuid()', () => { | ||
it('should generate a valid uuid using the web crypto api', () => { | ||
const result = generateUuid() | ||
|
||
expect(validUuidRegex.test(result)).toBe(true) | ||
}) | ||
|
||
it('should generate a valid uuid without the web crypto api', () => { | ||
Object.defineProperty(global, 'crypto', { | ||
configurable: true, | ||
value: { | ||
getRandomValues | ||
} | ||
}) | ||
|
||
const result = generateUuid() | ||
|
||
expect(validUuidRegex.test(result)).toBe(true) | ||
|
||
Object.defineProperty(global, 'crypto', { | ||
value: { | ||
getRandomValues, | ||
randomUUID | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Generates a UUID version 4 string. This function attempts to use the `crypto.randomUUID()` function from the Web | ||
* Crypto API if it is available, otherwise it uses a polyfill method. | ||
* | ||
* NOTE: `crypto.randomUUID()` is not available in non-secure contexts; only localhost and HTTPS. | ||
* @returns {string} a valid UUID version 4 string. | ||
* @see {@link https://stackoverflow.com/a/2117523} | ||
*/ | ||
export function generateUuid(): string { | ||
if (global.crypto.randomUUID) { | ||
return global.crypto.randomUUID() | ||
} | ||
|
||
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (value) => { | ||
const valueAsNumber: number = parseInt(value) | ||
|
||
return ( | ||
valueAsNumber ^ | ||
(global.crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (valueAsNumber / 4))) | ||
).toString(16) | ||
}) | ||
} |