fix(uuid): update uuid.ts based on JS implementation
Summary
Hi, this is Jake from the Technical Support team.
There was an issue of user properties being mapped incorrectly due to colliding UUIDs set by the Amplitude TS SDK[due to math.random function].
There was a fix in JS SDK but not carried over to TS SDK
- https://github.com/amplitude/Amplitude-JavaScript/blob/v8.x/src/uuid.js
The issue has been stale, so I am making PR for the issue!
Checklist
- [v] Does your PR title have the correct title format?
- Does your PR have a breaking change?: no
Hi @visiodeibc, TS’s analytics-core package is shared by both browser (analytics-browser package) and node (analytics-node package) environments in which crypto should be imported differently. It's predefined in browser environment but it requires to be imported in node environment.
I've committed a change to this.
NodeJS and Web both export "crypto" to their global scope (global.crypto and window.crypto, respectively) so it doesn't need to imported in NodeJS, it can just be executed from the global scope.
I added a change that if "global.crypto.getRandomValues" isn't there, then fall back to the legacy UUID.
@Mercy811 turns out you don't need to import "crypto" in a NodeJS environment. Crypto is exposed on the global scope (global.crypto or globalThis.crypto). Similar to how setTimeout, setInterval, etc... don't need to be imported as an NPM module.
@Mercy811 @daniel-graham-amplitude thank you so much for the fix! is this available in the latest SDK version?!
@visiodeibc it is yeah, it's been out for a few weeks. Thanks for the contribution!