Skip to content
Draft
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
21 changes: 15 additions & 6 deletions packages/core/src/tools/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/**
* UUID v4
* from https://gist.github.com/jed/982883
* Iterative implementation to avoid stack overflow from .replace() callback frames
* when the call stack is already deep (e.g. in Angular/React applications).
*/
export function generateUUID(placeholder?: string): string {
return placeholder
? // eslint-disable-next-line no-bitwise
(parseInt(placeholder, 10) ^ ((Math.random() * 16) >> (parseInt(placeholder, 10) / 4))).toString(16)
: `${1e7}-${1e3}-${4e3}-${8e3}-${1e11}`.replace(/[018]/g, generateUUID)
export function generateUUID(): string {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve generateUUID call signature compatibility

generateUUID is re-exported as part of @datadog/browser-core (packages/core/src/index.ts:156), but this change narrows its TypeScript signature from generateUUID(placeholder?: string) to zero arguments only. That is a source-compatible break for any typed consumer currently passing an argument (including undefined), which will now fail with “Expected 0 arguments” even though runtime behavior still accepts extra args. For a bugfix commit, this API break is unexpected and should be avoided by keeping the previous optional parameter in the signature.

Useful? React with 👍 / 👎.

const template = '10000000-1000-4000-8000-100000000000'
let result = ''
for (let i = 0; i < template.length; i++) {
const char = template[i]
if (char === '0' || char === '1' || char === '8') {
// eslint-disable-next-line no-bitwise
result += (parseInt(char, 10) ^ ((Math.random() * 16) >> (parseInt(char, 10) / 4))).toString(16)
} else {
result += char
}
}
return result
}

// Assuming input string is following the HTTP Cookie format defined in
Expand Down