Skip to content

Commit

Permalink
remove fallback UUID generation
Browse files Browse the repository at this point in the history
This is no longer needed as all browsers in the past few years have
window.crypto now.
Also removed duplicate test implementation of getRandomUuid().
  • Loading branch information
jkasten2 committed Oct 26, 2023
1 parent d924672 commit 65ec0ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 62 deletions.
28 changes: 0 additions & 28 deletions __test__/support/utils/Random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,4 @@ export default class Random {
),
);
}

public static getRandomUuid(): string {
let uuidStr = '';
const crypto =
typeof window === 'undefined'
? (global as any).crypto
: window.crypto || (<any>window).msCrypto;
if (crypto) {
uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
function (c) {
const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
},
);
} else {
uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
function (c) {
const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
},
);
}
return uuidStr;
}
}
28 changes: 14 additions & 14 deletions __test__/unit/helpers/configHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
AppUserConfig,
ConfigIntegrationKind,
} from '../../../src/shared/models/AppConfig';
import Random from '../../support/utils/Random';
import { getRandomUuid } from '../../../src/shared/utils/utils';
import { TestEnvironment } from '../../support/environment/TestEnvironment';
import { HttpHttpsEnvironment } from '../../support/models/HttpHttpsEnvironment';
import { getFinalAppConfig } from '../../support/helpers/configHelper';
Expand All @@ -24,7 +24,7 @@ describe('ConfigHelper Tests', () => {

test('promptOptions 1 - autoRegister = true backwards compatibility for custom integration shows native on HTTPS', async () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -46,7 +46,7 @@ describe('ConfigHelper Tests', () => {
);

const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -62,7 +62,7 @@ describe('ConfigHelper Tests', () => {

test('promptOptions 3 - autoRegister = false backwards compatibility for custom integration (no enabled prompts)', async () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: false,
};

Expand All @@ -78,7 +78,7 @@ describe('ConfigHelper Tests', () => {

test(`promptOptions 4 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};
(fakeUserConfig as any).promptOptions = {
Expand All @@ -105,7 +105,7 @@ describe('ConfigHelper Tests', () => {
);

const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -129,7 +129,7 @@ describe('ConfigHelper Tests', () => {

test(`promptOptions 6 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand Down Expand Up @@ -158,7 +158,7 @@ describe('ConfigHelper Tests', () => {
);

const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -183,7 +183,7 @@ describe('ConfigHelper Tests', () => {

test(`promptOptions 8 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -210,7 +210,7 @@ describe('ConfigHelper Tests', () => {

test(`promptOptions 9 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand Down Expand Up @@ -243,7 +243,7 @@ describe('ConfigHelper Tests', () => {
);

const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -270,7 +270,7 @@ describe('ConfigHelper Tests', () => {

test('autoResubscribe - autoRegister backwards compatibility for custom integration 1', () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: true,
};

Expand All @@ -287,7 +287,7 @@ describe('ConfigHelper Tests', () => {

test('autoResubscribe - autoRegister backwards compatibility for custom integration 2', () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
};

const fakeServerConfig = TestContext.getFakeServerAppConfig(
Expand All @@ -305,7 +305,7 @@ describe('ConfigHelper Tests', () => {

test('autoResubscribe - autoRegister backwards compatibility for custom integration 3', () => {
const fakeUserConfig: AppUserConfig = {
appId: Random.getRandomUuid(),
appId: getRandomUuid(),
autoRegister: false,
autoResubscribe: true,
};
Expand Down
28 changes: 8 additions & 20 deletions src/shared/utils/OneSignalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,18 @@ export class OneSignalUtils {
}

public static getRandomUuid(): string {
let uuidStr = '';
const crypto =
typeof window === 'undefined'
? (global as any).crypto
: window.crypto || (<any>window).msCrypto;
if (crypto) {
uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
function (c) {
const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
},
);
} else {
uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
function (c) {
const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
},
);
}
const uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
function (c) {
const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
},
);
return uuidStr;
}

Expand Down

0 comments on commit 65ec0ba

Please sign in to comment.