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 0e20353..a94ee41 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,15 +14,38 @@ $ 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 -uuid4.valid(id); // true +// Validate a UUID as proper V4 format (case-insensitive) +uuid4.valid(id); // true +``` + +### Direct in Browser or Deno + +``` +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 \ No newline at end of file +ISC 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..89d5275 --- /dev/null +++ b/index.mjs @@ -0,0 +1,19 @@ +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) { + 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..60f98c5 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,13 @@ { "name": "uuid4", - "version": "1.1.4", + "version": "2.0.3", "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" @@ -20,4 +24,4 @@ "url": "https://github.com/tracker1/node-uuid4/issues" }, "homepage": "https://github.com/tracker1/node-uuid4" -} +} \ No newline at end of file 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));