Reputation: 4333
I'm using Zapier's code module to write some Nodejs to generate a UUID.
I know that I can use node's crypto "library" because I've used it in the past to hash some text (MD5) to send to an API endpoint. I also know that I can use the crypto function randomInt.
For example, the following will correctly without errors return a number in the stated range:
const crypto = require('crypto');
let num = crypto.randomInt(1, 7);
output = [{id: num}];
When I try to use the randomUUID function (documentation), like the following, I get the error "TypeError: crypto.randomUUID is not a function" from Zapier:
const crypto = require('crypto');
let uuid = crypto.randomUUID();
output = [{id: uuid}];
I almost gave up there, but tried one last weird thing:
const crypto = require('crypto');
let uuid = crypto.randomUUID; //changed this
output = [{id: uuid}];
Note that I removed the ()
and this didn't throw an error and returned something like:
LT6TvivKgpFu5Yk3OvQmti1Hq1aNy5ZM
That looks a lot like a proper UUID since it has the right number of characters, but not the expected hyphens like (for example) 88368f2a-d5db-47d8-a05f-534fab0a0045
So, two questions:
randomUUID()
is not a function? Does it not support this or am I coding something weirdly wrong or not requiring something needed? Is there a different way to do this?randomUUID
(no ()
) is this actually a reliable UUID?EDIT: more info from Zapier, they are saying that their logs show that the code step cannot find module 'uuid'
Upvotes: 0
Views: 525
Reputation: 1
For future reference for anyone else with this issue, this is how I was able to get it to work:
Use: Run Javascript in Code by Zapier
Insert this code:
function generateRandomUUID() {
const hexChars = '0123456789abcdef';
let result = '';
const crypto = (typeof window === 'undefined' && typeof require === 'function')
? require('crypto').webcrypto
: (window.crypto || window.msCrypto);
if (!crypto) {
throw new Error('Crypto support is not available in this environment');
}
// Create a typed array for secure random values
const randomValues = new Uint8Array(16);
// Fill the typed array with random values
crypto.getRandomValues(randomValues);
// Set the version to 4 (0b0100) in the 13th character
randomValues[6] = (randomValues[6] & 0x0f) | 0x40;
// Set the variant to DCE 1.1 (0b1000) in the 17th character
randomValues[8] = (randomValues[8] & 0x3f) | 0x80;
// Define the positions for the hyphens
const hyphenPositions = [8, 13, 18, 23];
for (let i = 0; i < randomValues.length; i++) {
// Convert to hexadecimal and ensure it is two characters
let hex = randomValues[i].toString(16).padStart(2, '0');
result += hex;
// Insert hyphens at the correct positions
if (hyphenPositions.includes(result.length)) {
result += '-';
}
}
return result;
}
// Ensure the output object is defined
if (typeof output === 'undefined') {
var output = {};
}
// Generate a random UUID and assign it to the output object
output.randomUUID = generateRandomUUID();
console.log(output);
Upvotes: 0
Reputation: 33
I stumbled across this post looking for a way to generate a random UUID from within a Zapier Code step. While none of what's on this page actually worked for me, I was able to find a solution. I wanted to share it in case it helps someone else.
import uuid
# Generate a random UUID
random_uuid = uuid.uuid4()
# Convert the UUID to a string
random_uuid_str = str(random_uuid)
print(random_uuid_str)
Upvotes: 1
Reputation: 11
When I use randomUUID (no ()) is this actually a reliable UUID?
The value you're getting there is not a reliable UUID. UUIDs will contain only 0-9a-f characters, the example you have there doesn't match.
Sorry I can't help you with what's going wrong with the other part! You could maybe try something like this
let crypto;
try {
crypto = require('crypto');
} catch (err) {
console.log('crypto support is disabled!');
}
to make sure the crypto
package is actually available and there's not some error being silently suppressed? You may also want to check the version, as randomUUID()
appears to have been added in v14.17.0
Also nit but should probably be const uuid = crypto.randomUUID();
instead of let
;)
Upvotes: 1