Skip to content

Commit

Permalink
Use crypto.randomUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshaf committed Nov 16, 2024
1 parent e154c57 commit b30cb6e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 28 deletions.
5 changes: 2 additions & 3 deletions packages/idb-cache/src/encryptionTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
DecryptionError,
IDBCacheError,
} from "./errors";
import { generateUUIDv1 } from "./utils";

/**
* Encrypts a chunk of data using the worker.
Expand All @@ -22,7 +21,7 @@ export async function encryptChunk(
value: string,
pendingRequests: Map<string, ExtendedPendingRequest<EncryptedChunk>>
): Promise<EncryptedChunk> {
const requestId = generateUUIDv1();
const requestId = crypto.randomUUID();
try {
const encrypted = await sendMessageToWorker<"encrypt">(
port,
Expand Down Expand Up @@ -66,7 +65,7 @@ export async function decryptChunk(
ciphertext: ArrayBuffer,
pendingRequests: Map<string, ExtendedPendingRequest<string>>
): Promise<string> {
const requestId = generateUUIDv1();
const requestId = crypto.randomUUID();
try {
const decrypted = await sendMessageToWorker<"decrypt">(
port,
Expand Down
25 changes: 0 additions & 25 deletions packages/idb-cache/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@ import {
} from "idb";
import type { IDBCacheSchema, STORE } from "./types";

/**
* Generates a UUID v1-like string.
* @returns A UUID v1-like string.
*/
export function generateUUIDv1(): string {
const now = Date.now();

const timeLow = (now & 0xffffffff).toString(16).padStart(8, "0");
const timeMid = ((now / 0x100000000) & 0xffff).toString(16).padStart(4, "0");
const timeHiAndVersion = (((now / 0x1000000000000) & 0x0fff) | 0x1000)
.toString(16)
.padStart(4, "0");

const clockSeq = (Math.floor(Math.random() * 0x3fff) | 0x8000)
.toString(16)
.padStart(4, "0");

const node = crypto.getRandomValues(new Uint8Array(6));
const nodeHex = Array.from(node)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");

return `${timeLow}-${timeMid}-${timeHiAndVersion}-${clockSeq}-${nodeHex}`;
}

const uuidCache = new Map<string, string>();

/**
Expand Down

0 comments on commit b30cb6e

Please sign in to comment.