From 082a0b3eef27460d9572fffad667f62f875f07ea Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Tue, 13 Jun 2017 08:38:39 -0700 Subject: [PATCH] v5 support in CLI (#197) --- bin/uuid | 58 +++++++++++++++++++++++++++++++++++++++----------------- v5.js | 2 +- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/bin/uuid b/bin/uuid index f732e991..2fd26d76 100755 --- a/bin/uuid +++ b/bin/uuid @@ -1,26 +1,50 @@ #!/usr/bin/env node +var assert = require('assert'); -var path = require('path'); -var uuid = require(path.join(__dirname, '..')); +function usage() { + console.log('Usage:'); + console.log(' uuid'); + console.log(' uuid v1'); + console.log(' uuid v4'); + console.log(' uuid v5 '); + console.log(' uuid --help'); + console.log('\nNote: may be "URL" or "DNS" to use the corresponding UUIDs defined by RFC4122'); +} -var arg = process.argv[2]; +var args = process.argv.slice(2); -if ('--help' === arg) { - console.log('\n USAGE: uuid [version] [options]\n\n'); - console.log(' options:\n'); - console.log(' --help Display this message and exit\n'); +if (args.indexOf('--help') >= 0) { + usage(); process.exit(0); } +var version = args.shift() || 'v4'; -if (null == arg) { - console.log(uuid()); - process.exit(0); -} +switch (version) { + case 'v1': + var uuidV1 = require('../v1'); + console.log(uuidV1()); + break; -if ('v1' !== arg && 'v4' !== arg) { - console.error('Version must be RFC4122 version 1 or version 4, denoted as "v1" or "v4"'); - process.exit(1); -} + case 'v4': + var uuidV4 = require('../v4'); + console.log(uuidV4()); + break; + + case 'v5': + var uuidV5 = require('../v5'); -console.log(uuid[arg]()); -process.exit(0); + var name = args.shift(); + var namespace = args.shift(); + assert(name != null, 'v5 name not specified'); + assert(namespace != null, 'v5 namespace not specified'); + + if (namespace == 'URL') namespace = uuidV5.URL; + if (namespace == 'DNS') namespace = uuidV5.DNS; + + console.log(uuidV5(name, namespace)); + break; + + default: + usage(); + process.exit(1); +} diff --git a/v5.js b/v5.js index 0fc42ff4..39cc35e7 100644 --- a/v5.js +++ b/v5.js @@ -25,7 +25,7 @@ function v5(name, namespace, buf, offset) { if (typeof(namespace) == 'string') namespace = uuidToBytes(namespace); if (!Array.isArray(name)) throw TypeError('name must be an array of bytes'); - if (!Array.isArray(namespace) || namespace.length != 16) throw TypeError('namespace must be an array of bytes'); + if (!Array.isArray(namespace) || namespace.length != 16) throw TypeError('namespace must be uuid string or an Array of 16 byte values'); // Per 4.3 var bytes = sha1(namespace.concat(name));