Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kibisis): replace global object with window #139

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/clients/kibisis/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { generateUuid } from './utils'
import { randomUUID, getRandomValues } from 'crypto'
import { getRandomValues, randomUUID } 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

beforeAll(() => {
Object.defineProperty(window, 'crypto', {
configurable: true,
value: {
getRandomValues,
randomUUID
}
})
})

describe('generateUuid()', () => {
it('should generate a valid uuid using the web crypto api', () => {
const result = generateUuid()
Expand All @@ -13,23 +23,15 @@ describe(`${__dirname}/utils`, () => {
})

it('should generate a valid uuid without the web crypto api', () => {
Object.defineProperty(global, 'crypto', {
configurable: true,
value: {
getRandomValues
}
})
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.crypto.randomUUID

const result = generateUuid()

expect(validUuidRegex.test(result)).toBe(true)

Object.defineProperty(global, 'crypto', {
value: {
getRandomValues,
randomUUID
}
})
window.crypto.randomUUID = randomUUID
})
})
})
6 changes: 3 additions & 3 deletions src/clients/kibisis/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* @see {@link https://stackoverflow.com/a/2117523}
*/
export function generateUuid(): string {
if (global.crypto.randomUUID) {
return global.crypto.randomUUID()
if (window.crypto.randomUUID) {
return window.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)))
(window.crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (valueAsNumber / 4)))
).toString(16)
})
}
12 changes: 1 addition & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,7 @@
resolved "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz"
integrity sha512-SOSkfJDddaM7mak6cPEpswyTRnuRltl429hMraQEglW+OkovnCzsiszTmsrlY//qLFjCpQDFRvjdm2wA5pPm9w==

"@babel/plugin-proposal-private-property-in-object@^7.12.1":
version "7.21.11"
resolved "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.11.tgz"
integrity sha512-0QZ8qP/3RLDVBwBFoWAwCtgcDZJVwA5LUJRZU8x2YFfKNuFq161wK3cuGrALu5yiPu+vzwTAg/sMWVNeWeNyaw==
dependencies:
"@babel/helper-annotate-as-pure" "^7.18.6"
"@babel/helper-create-class-features-plugin" "^7.21.0"
"@babel/helper-plugin-utils" "^7.20.2"
"@babel/plugin-syntax-private-property-in-object" "^7.14.5"

"@babel/plugin-proposal-private-property-in-object@^7.21.11":
"@babel/plugin-proposal-private-property-in-object@^7.12.1", "@babel/plugin-proposal-private-property-in-object@^7.21.11":
version "7.21.11"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.11.tgz#69d597086b6760c4126525cfa154f34631ff272c"
integrity sha512-0QZ8qP/3RLDVBwBFoWAwCtgcDZJVwA5LUJRZU8x2YFfKNuFq161wK3cuGrALu5yiPu+vzwTAg/sMWVNeWeNyaw==
Expand Down