Is there any built-in mechanism in SuiteScript to ...
# suitescript
s
Is there any built-in mechanism in SuiteScript to generate a UUID/GUID? If not, are there any preferred libraries for it? I just need to generate one for a messaging queue that needs a unique request id as a UUID
The uuid package in npm looks promising, and has zero dependencies, but if there’s already some method built into NetSuite, that would be preferable.
b
you can abuse Form.addCredentialField since it returns guids
💯 1
s
Awesome, thanks for the suggestion!
b
else pick your favored uuid library, though someone failed to use uuid since it uses crypto.getRandomValues
s
Yeah, I was wondering if it used some module that wasn’t available or exposed via the SuiteScript engine, similar to how Luxon requires Intl
a
umm what message queue? Kafka? when i had to do that they wanted a specific UUID format.. but it was easily implemented with google search
Copy code
function uuidv4() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
        }
s
I thought
Math.random()
wasn't generally considered random enough for something like UUID generation?
s
It's an internally built message queue, though because it's in use by many apps already, the requirements can't be changed for it without a lot of impact. And yes, Math.random is not the best choice if there's a better alternative. I still doubt we have duplicate UUID's even if I used it, though
It's mainly for duplicate message avoidance, and logging
a
Math.random()
isn't good enough for crpyto type stuff, but for collision avoidance it shouldn't be an issue... I think there's something like 4 trillion permutations, so collisions are ... unlikely... I just did what I was told by the ppl that wanted the message tagged with a UUID, they were happy with the solution.
b
actually test the collision thing
the random number generator isnt usually good enough to prevent collisions when you start getting to the tens of thousands
a
I'm not sure what you mean here, you can't put an upper bound on the number of attempts to hit a collision, without taking into account the size of the string you're making. its not 1 call to
Math.random()
its 32 of them... for all 32 to be a match, isn't in the range of 10^4, its > 10^30
honestly this shared wisdom around
Math.random()
being terrible is impressive PR. I think the bigger takeaway should be don't try to make your own crypto solution, and when you need a random number
Math.random()
is generally fine. The 2 exceptions being, 1. actual crypto and 2. where you need a seed so the seed can be shared and reused and the outcome deterministic.
b
its not a theoretical thing, that implies you know how good the random number generator is
run the uuid 4 generating code for 100k codes, count the number of collisions
if the number of collisions you see is too high for your purposes, choose a different method