diff --git a/src/core/operations/GenerateUUID.mjs b/src/core/operations/GenerateUUID.mjs
index 1ee0faba..cb2373d9 100644
--- a/src/core/operations/GenerateUUID.mjs
+++ b/src/core/operations/GenerateUUID.mjs
@@ -5,8 +5,8 @@
*/
import Operation from "../Operation.mjs";
-import crypto from "crypto";
-
+import * as uuid from "uuid";
+import OperationError from "../errors/OperationError.mjs";
/**
* Generate UUID operation
*/
@@ -20,11 +20,24 @@ class GenerateUUID extends Operation {
this.name = "Generate UUID";
this.module = "Crypto";
- this.description = "Generates an RFC 4122 version 4 compliant Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).
A version 4 UUID relies on random numbers, in this case generated using window.crypto
if available and falling back to Math.random
if not.";
+ this.description = "Generates an RFC 4122 compliant Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).
A version 4 UUID relies on random numbers, in this case generated using uuid
package";
this.infoURL = "https://wikipedia.org/wiki/Universally_unique_identifier";
this.inputType = "string";
this.outputType = "string";
- this.args = [];
+ this.args = [
+ {
+ name: "UUID Version",
+ type: "option",
+ value: [
+ "v1", "v3", "v4", "v5"
+ ]
+ },
+ {
+ name: "UUID namespace (valid for v3 and v5)",
+ type: "string",
+ value: "1b671a64-40d5-491e-99b0-da01ff1f3341"
+ }
+ ];
}
/**
@@ -33,16 +46,19 @@ class GenerateUUID extends Operation {
* @returns {string}
*/
run(input, args) {
- const buf = new Uint32Array(4).map(() => {
- return crypto.randomBytes(4).readUInt32BE(0, true);
- });
- let i = 0;
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
- const r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
- v = c === "x" ? r : (r & 0x3 | 0x8);
- i++;
- return v.toString(16);
- });
+ const [version, namespace] = args;
+ const hasDesiredVersion = typeof uuid[version] === "function";
+ if (!hasDesiredVersion) throw new OperationError("Invalid UUID version");
+
+ const versionThatRequiresNamespace = ["v3", "v5"];
+
+ const requiresNamespace = versionThatRequiresNamespace.includes(version);
+ if (!requiresNamespace) return uuid[version]();
+
+ const hasValidNamespace = typeof namespace === "string" && uuid.validate(namespace);
+ if (!hasValidNamespace) throw new OperationError("Invalid UUID namespace");
+
+ return uuid[version](input, namespace);
}
}