2018-05-21 12:35:11 +00:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
2023-03-21 18:38:21 -03:00
import * as uuid from "uuid" ;
import OperationError from "../errors/OperationError.mjs" ;
2018-05-21 12:35:11 +00:00
/**
* Generate UUID operation
*/
class GenerateUUID extends Operation {
/**
* GenerateUUID constructor
*/
constructor () {
super ();
this . name = "Generate UUID" ;
this . module = "Crypto" ;
2023-03-21 18:38:21 -03:00
this . description = "Generates an RFC 4122 compliant Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).<br><br>A version 4 UUID relies on random numbers, in this case generated using <code>uuid</code> package" ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/Universally_unique_identifier" ;
2018-05-21 12:35:11 +00:00
this . inputType = "string" ;
this . outputType = "string" ;
2023-03-21 18:38:21 -03:00
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"
}
];
2018-05-21 12:35:11 +00:00
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run ( input , args ) {
2023-03-21 18:38:21 -03:00
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 );
2018-05-21 12:35:11 +00:00
}
}
export default GenerateUUID ;