All files / src/services deviceInstallation.ts

75% Statements 12/16
50% Branches 2/4
66.66% Functions 2/3
75% Lines 12/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 283x   3x     1x 1x 1x                   3x 2x 2x 1x     1x 1x 1x    
import * as SecureStore from 'expo-secure-store';
 
const DEVICE_INSTALLATION_ID_KEY = 'device_installation_id';
 
function newInstallationID(): string {
  const randomUUID = globalThis.crypto?.randomUUID?.bind(globalThis.crypto);
  if (randomUUID) {
    return randomUUID();
  }
 
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (char) => {
    const value = Math.floor(Math.random() * 16);
    const nibble = char === 'x' ? value : (value & 0x3) | 0x8;
    return nibble.toString(16);
  });
}
 
export async function getDeviceInstallationID(): Promise<string> {
  const existing = await SecureStore.getItemAsync(DEVICE_INSTALLATION_ID_KEY);
  if (existing) {
    return existing;
  }
 
  const installationID = newInstallationID();
  await SecureStore.setItemAsync(DEVICE_INSTALLATION_ID_KEY, installationID);
  return installationID;
}