-
-
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
- Loading branch information
1 parent
380bd15
commit 3c13026
Showing
4 changed files
with
64 additions
and
12 deletions.
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
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,32 @@ | ||
import { generateUuid } from './utils' | ||
|
||
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', () => { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const cryptoRandomUUID = crypto.randomUUID | ||
|
||
Object.defineProperty(crypto, 'randomUUID', { | ||
configurable: true, | ||
value: undefined | ||
}) | ||
|
||
const result = generateUuid() | ||
|
||
expect(validUuidRegex.test(result)).toBe(true) | ||
|
||
Object.defineProperty(crypto, 'randomUUID', { | ||
value: cryptoRandomUUID | ||
}) | ||
}) | ||
}) | ||
}) |
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 (crypto.randomUUID) { | ||
return crypto.randomUUID() | ||
} | ||
|
||
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (value) => { | ||
const valueAsNumber: number = parseInt(value) | ||
|
||
return ( | ||
valueAsNumber ^ | ||
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (valueAsNumber / 4))) | ||
).toString(16) | ||
}) | ||
} |