Skip to content

Commit d24faf3

Browse files
committed
fix(randomUUID): add fallback when crypto.randomUUID is unavailable (HTTP/older envs)
In some environments (non-secure origins like http, older browsers, certain test runners), `crypto.randomUUID` is not available, causing runtime errors. This change preserves the existing fast path when `crypto.randomUUID` exists, and adds a RFC4122 v4-shaped fallback for other cases: - Detects availability via: typeof crypto !== 'undefined' && crypto.randomUUID - Uses `crypto.randomUUID.bind(crypto)` when present - Otherwise formats a UUID using 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(...) with Math.random, ensuring version (4) and variant (8|9|a|b) bits - Keeps strict typing with template-literal UUID type for DX No breaking changes.
1 parent ea2328e commit d24faf3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/agents-core/src/shims/shims-browser.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,14 @@ export class BrowserEventEmitter<
8282

8383
export { BrowserEventEmitter as RuntimeEventEmitter };
8484

85-
export const randomUUID = crypto.randomUUID.bind(crypto);
85+
export const randomUUID: () => `${string}-${string}-${string}-${string}-${string}` =
86+
(typeof crypto !== 'undefined' && crypto.randomUUID)
87+
? crypto.randomUUID.bind(crypto)
88+
: () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
89+
const r = Math.random() * 16 | 0;
90+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
91+
return v.toString(16);
92+
}) as `${string}-${string}-${string}-${string}-${string}`;
8693
export const Readable = class Readable {
8794
constructor() {}
8895
pipeTo(

0 commit comments

Comments
 (0)