From fd416501a632e2c68022593731651f55acfc6424 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 12:46:54 -0700 Subject: [PATCH 01/10] Update README.md --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e20353..308c98e 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,20 @@ var id = uuid4(); uuid4.valid(id); // true ``` +## Alternative (browser-only) + +If you *only* care about browser use and want to avoid modules, you may get by with the following snippet. + +```javascript + // CC0 - based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html + function uuid() { + var temp_url = URL.createObjectURL(new Blob()); + var uuid = temp_url.toString(); + URL.revokeObjectURL(temp_url); + return uuid.split(/[:\/]/g).pop(); // remove prefixes + } +``` + ## License -ISC License \ No newline at end of file +ISC License From c4662c0da3dd8631d74b7ed41b986bbfb548b4dc Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 12:47:20 -0700 Subject: [PATCH 02/10] Update README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 308c98e..d7bec55 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,13 @@ uuid4.valid(id); // true If you *only* care about browser use and want to avoid modules, you may get by with the following snippet. ```javascript - // CC0 - based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html - function uuid() { - var temp_url = URL.createObjectURL(new Blob()); - var uuid = temp_url.toString(); - URL.revokeObjectURL(temp_url); - return uuid.split(/[:\/]/g).pop(); // remove prefixes - } +// CC0 - based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html +function uuid() { + var temp_url = URL.createObjectURL(new Blob()); + var uuid = temp_url.toString(); + URL.revokeObjectURL(temp_url); + return uuid.split(/[:\/]/g).pop(); // remove prefixes +} ``` ## License From 665e24c974ce6bd62e5c414fc4df0fb908437d4a Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 12:51:29 -0700 Subject: [PATCH 03/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d7bec55..e0c672b 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ function uuid() { var temp_url = URL.createObjectURL(new Blob()); var uuid = temp_url.toString(); URL.revokeObjectURL(temp_url); - return uuid.split(/[:\/]/g).pop(); // remove prefixes + return uuid.split(/[:\/]/g).pop().toLowerCase(); // remove prefixes } ``` From ae6540346362b141601dbaa03be96f674d402ada Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 12:51:47 -0700 Subject: [PATCH 04/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e0c672b..fb05e62 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ uuid4.valid(id); // true If you *only* care about browser use and want to avoid modules, you may get by with the following snippet. ```javascript -// CC0 - based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html +// Based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html function uuid() { var temp_url = URL.createObjectURL(new Blob()); var uuid = temp_url.toString(); From 333cbdc3d22de0ccecda774155968864a3509a27 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 13:08:02 -0700 Subject: [PATCH 05/10] v2.0.0 - cleanup, remove legacy, remove async node implementation, add module version(s) --- README.md | 21 ++++------------ browser.js | 69 +++++++++++----------------------------------------- browser.mjs | 17 +++++++++++++ index.js | 44 +++++++++++---------------------- index.mjs | 19 +++++++++++++++ package.json | 8 ++++-- 6 files changed, 75 insertions(+), 103 deletions(-) create mode 100644 browser.mjs create mode 100644 index.mjs diff --git a/README.md b/README.md index fb05e62..a8af6c3 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ A Node.js module for generating and validation V4 UUIDs +NOTE: as of Version 2, legacy browsers are no longer supported, you can keep using 1.x if you need +to support modern and legacy browsers. + ## Install ```bash @@ -11,27 +14,13 @@ $ npm install uuid4 ## Usage ```javascript -var uuid4 = require('uuid4'); +var uuid4 = require("uuid4"); // Generate a new UUID var id = uuid4(); // Validate a UUID as proper V4 format -uuid4.valid(id); // true -``` - -## Alternative (browser-only) - -If you *only* care about browser use and want to avoid modules, you may get by with the following snippet. - -```javascript -// Based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html -function uuid() { - var temp_url = URL.createObjectURL(new Blob()); - var uuid = temp_url.toString(); - URL.revokeObjectURL(temp_url); - return uuid.split(/[:\/]/g).pop().toLowerCase(); // remove prefixes -} +uuid4.valid(id); // true ``` ## License diff --git a/browser.js b/browser.js index a485759..a7f7872 100644 --- a/browser.js +++ b/browser.js @@ -1,57 +1,16 @@ -(function() { - function getBytes() { - try { - // Modern Browser - return Array.from( - (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(16)) - ); - } catch (error) { - // Legacy Browser, fallback to Math.random - var ret = []; - while (ret.length < 16) ret.push((Math.random() * 256) & 0xff); - return ret; - } - } +const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; +function valid(uuid) { + return uuidPattern.test(uuid); +} - function m(v) { - v = v.toString(16); - if (v.length < 2) v = "0" + v; - return v; - } +// Based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html +// IE11 and Modern Browsers Only +function uuid4() { + var temp_url = URL.createObjectURL(new Blob()); + var uuid = temp_url.toString(); + URL.revokeObjectURL(temp_url); + return uuid.split(/[:\/]/g).pop().toLowerCase(); // remove prefixes +} +uuid4.valid = valid; - function genUUID() { - var rnd = getBytes(); - rnd[6] = (rnd[6] & 0x0f) | 0x40; - rnd[8] = (rnd[8] & 0x3f) | 0x80; - rnd = rnd - .map(m) - .join("") - .match(/(.{8})(.{4})(.{4})(.{4})(.{12})/); - rnd.shift(); - return rnd.join("-"); - } - - var uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; - function isUUID(uuid) { - return uuidPattern.test(uuid); - } - - genUUID.valid = isUUID; - - // global - if (window) { - window.uuid4 = genUUID; - } - - // Node-style CJS - if (typeof module !== "undefined" && module.exports) { - module.exports = genUUID; - } - - // AMD - legacy - if (typeof define === "function" && define.amd) { - define([], function() { - return genUUID; - }); - } -})(); +module.exports = uuid4; diff --git a/browser.mjs b/browser.mjs new file mode 100644 index 0000000..9010f16 --- /dev/null +++ b/browser.mjs @@ -0,0 +1,17 @@ +const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; +function valid(uuid) { + return uuidPattern.test(uuid); +} + +// Based on https://abhishekdutta.org/blog/standalone_uuid_generator_in_javascript.html +// IE11 and Modern Browsers Only +function uuid4() { + var temp_url = URL.createObjectURL(new Blob()); + var uuid = temp_url.toString(); + URL.revokeObjectURL(temp_url); + return uuid.split(/[:\/]/g).pop().toLowerCase(); // remove prefixes +} +uuid4.valid = valid; + +export default uuid4; +export { uuid4, valid }; diff --git a/index.js b/index.js index 97a3ff5..0af739e 100644 --- a/index.js +++ b/index.js @@ -1,34 +1,18 @@ -"use strict"; +const crypto = require("crypto"); -var crypto = require("crypto"), - uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; - -exports = module.exports = genUUID; -exports.valid = isUUID; - -function genUUID(callback) { - if (typeof callback !== "function") { - var rnd = crypto.randomBytes(16); - rnd[6] = (rnd[6] & 0x0f) | 0x40; - rnd[8] = (rnd[8] & 0x3f) | 0x80; - rnd = rnd.toString("hex").match(/(.{8})(.{4})(.{4})(.{4})(.{12})/); - rnd.shift(); - return rnd.join("-"); - } - crypto.randomBytes(16, function(err, rnd) { - if (err) return callback(err); - try { - rnd[6] = (rnd[6] & 0x0f) | 0x40; - rnd[8] = (rnd[8] & 0x3f) | 0x80; - rnd = rnd.toString("hex").match(/(.{8})(.{4})(.{4})(.{4})(.{12})/); - rnd.shift(); - return callback(null, rnd.join("-")); - } catch (err2) { - return callback(err2); - } - }); +const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; +function valid(uuid) { + return uuidPattern.test(uuid); } -function isUUID(uuid) { - return uuidPattern.test(uuid); +function uuid4() { + var rnd = crypto.randomBytes(16); + rnd[6] = (rnd[6] & 0x0f) | 0x40; + rnd[8] = (rnd[8] & 0x3f) | 0x80; + rnd = rnd.toString("hex").match(/(.{8})(.{4})(.{4})(.{4})(.{12})/); + rnd.shift(); + return rnd.join("-"); } +uuid4.valid = valid; + +module.exports = uuid4; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..e034535 --- /dev/null +++ b/index.mjs @@ -0,0 +1,19 @@ +const crypto = require("crypto"); + +const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; +function valid(uuid) { + return uuidPattern.test(uuid); +} + +function uuid4() { + var rnd = crypto.randomBytes(16); + rnd[6] = (rnd[6] & 0x0f) | 0x40; + rnd[8] = (rnd[8] & 0x3f) | 0x80; + rnd = rnd.toString("hex").match(/(.{8})(.{4})(.{4})(.{4})(.{12})/); + rnd.shift(); + return rnd.join("-"); +} +uuid4.valid = valid; + +export default uuid4; +export { uuid4, valid }; diff --git a/package.json b/package.json index 71506e6..760b40c 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,13 @@ { "name": "uuid4", - "version": "1.1.4", + "version": "2.0.0", "description": "Node UUID v4 Generator", "main": "index.js", - "browser": "browser.js", + "module": "index.mjs", + "browser": { + "./index.js": "browser.js", + "./index.mjs": "browser.mjs" + }, "repository": { "type": "git", "url": "https://github.com/tracker1/node-uuid4.git" From efba6618e84d6976182cc5f2a1eb6bdfaeaba5fa Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 13:12:02 -0700 Subject: [PATCH 06/10] fix node module version --- index.mjs | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.mjs b/index.mjs index e034535..89d5275 100644 --- a/index.mjs +++ b/index.mjs @@ -1,4 +1,4 @@ -const crypto = require("crypto"); +import crypto from "crypto"; const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; function valid(uuid) { diff --git a/package.json b/package.json index 760b40c..00efecf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid4", - "version": "2.0.0", + "version": "2.0.1", "description": "Node UUID v4 Generator", "main": "index.js", "module": "index.mjs", From 7880fba3e558d20a7fa5f043264192f1c2e8ca30 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 13:14:21 -0700 Subject: [PATCH 07/10] update documentation --- LICENSE | 2 +- README.md | 4 ++-- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 23e3b59..c385123 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2014, Michael J. Ryan +Copyright (c) 2020, Michael J. Ryan Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. diff --git a/README.md b/README.md index a8af6c3..615af3c 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,12 @@ $ npm install uuid4 ## Usage ```javascript -var uuid4 = require("uuid4"); +import uuid4 from "uuid4"; // Generate a new UUID var id = uuid4(); -// Validate a UUID as proper V4 format +// Validate a UUID as proper V4 format (case-insensitive) uuid4.valid(id); // true ``` diff --git a/package.json b/package.json index 00efecf..9ea9992 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid4", - "version": "2.0.1", + "version": "2.0.2", "description": "Node UUID v4 Generator", "main": "index.js", "module": "index.mjs", From 4f06ffac635b83b028811ebfe0f6500751bc6041 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 13 Jun 2020 13:56:31 -0700 Subject: [PATCH 08/10] additional usage notes, ref deno std implementation --- README.md | 23 +++++++++++++++++++++++ temp.ts | 5 +++++ 2 files changed, 28 insertions(+) create mode 100644 temp.ts diff --git a/README.md b/README.md index 615af3c..9e996df 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,29 @@ var id = uuid4(); uuid4.valid(id); // true ``` +### Direct in Browser + +``` +import uuid4 from 'https://cdn.jsdelivr.net/gh/tracker1/node-uuid4/browser.mjs'; + +// or + +const { default: uuid4 } = await import('https://cdn.jsdelivr.net/gh/tracker1/node-uuid4/browser.mjs') +``` + +### Deno + +Use the canonical implementation instead. + +``` +import { v4 as uuid4 } from "https://deno.land/std/uuid/mod.ts"; + +const id = uuid4.generate(); + +console.log(id); +console.log(uuid4.validate(id)); +``` + ## License ISC License diff --git a/temp.ts b/temp.ts new file mode 100644 index 0000000..4395990 --- /dev/null +++ b/temp.ts @@ -0,0 +1,5 @@ +import { v4 as uuid4 } from "https://deno.land/std/uuid/mod.ts"; + +const id = uuid4.generate(); +console.log(id); +console.log(uuid4.validate(id)); From 02cb1f986f4bd2d81872d913aeaa8c26bb026066 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 6 Aug 2022 20:41:43 -0700 Subject: [PATCH 09/10] Add Deno reference to README.md Add reference to Deno for use in Readme (Matches modern browser usage). --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e996df..a94ee41 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ var id = uuid4(); uuid4.valid(id); // true ``` -### Direct in Browser +### Direct in Browser or Deno ``` import uuid4 from 'https://cdn.jsdelivr.net/gh/tracker1/node-uuid4/browser.mjs'; From 909809d0cc208d04435867bf0c8c0c70182bd0a1 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Sat, 6 Aug 2022 20:49:39 -0700 Subject: [PATCH 10/10] Patch for building with Webpack. Fix browser reference paths. Fixes #8 --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9ea9992..60f98c5 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "uuid4", - "version": "2.0.2", + "version": "2.0.3", "description": "Node UUID v4 Generator", "main": "index.js", "module": "index.mjs", "browser": { - "./index.js": "browser.js", - "./index.mjs": "browser.mjs" + "./index.js": "./browser.js", + "./index.mjs": "./browser.mjs" }, "repository": { "type": "git", @@ -24,4 +24,4 @@ "url": "https://github.com/tracker1/node-uuid4/issues" }, "homepage": "https://github.com/tracker1/node-uuid4" -} +} \ No newline at end of file