diff --git a/CHANGELOG.md b/CHANGELOG.md index bcefac2f..7519d19d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [8.3.2](https://github.com/uuidjs/uuid/compare/v8.3.1...v8.3.2) (2020-12-08) + +### Bug Fixes + +- lazy load getRandomValues ([#537](https://github.com/uuidjs/uuid/issues/537)) ([16c8f6d](https://github.com/uuidjs/uuid/commit/16c8f6df2f6b09b4d6235602d6a591188320a82e)), closes [#536](https://github.com/uuidjs/uuid/issues/536) + ### [8.3.1](https://github.com/uuidjs/uuid/compare/v8.3.0...v8.3.1) (2020-10-04) ### Bug Fixes diff --git a/README.md b/README.md index 6cea0393..ed27e576 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ For the creation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDs - Node 8, 10, 12, 14 - Chrome, Safari, Firefox, Edge, IE 11 browsers - Webpack and rollup.js module bundlers - - [React Native / Expo](#react-native-expo) + - [React Native / Expo](#react-native--expo) - **Secure** - Cryptographically-strong random values - **Small** - Zero-dependency, small footprint, plays nice with "tree shaking" packagers - **CLI** - Includes the [`uuid` command line](#command-line) utility @@ -246,7 +246,7 @@ uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836' ### uuid.v5(name, namespace[, buffer[, offset]]) -Createa an RFC version 5 (namespace w/ SHA-1) UUID +Create an RFC version 5 (namespace w/ SHA-1) UUID | | | | --- | --- | @@ -296,6 +296,23 @@ uuidValidate('not a UUID'); // ⇨ false uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true ``` +Using `validate` and `version` together it is possible to do per-version validation, e.g. validate for only v4 UUIds. + +```javascript +import { version as uuidVersion } from 'uuid'; +import { validate as uuidValidate } from 'uuid'; + +function uuidValidateV4(uuid) { + return uuidValidate(uuid) && uuidVersion(uuid) === 4; +} + +const v1Uuid = 'd9428888-122b-11e1-b85c-61cd3cbb3210'; +const v4Uuid = '109156be-c4fb-41ea-b1b4-efe1671c5836'; + +uuidValidateV4(v4Uuid); // ⇨ true +uuidValidateV4(v1Uuid); // ⇨ false +``` + ### uuid.version(str) Detect RFC version of a UUID diff --git a/README_js.md b/README_js.md index 6dbd05b9..0e858a47 100644 --- a/README_js.md +++ b/README_js.md @@ -24,7 +24,7 @@ For the creation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDs - Node 8, 10, 12, 14 - Chrome, Safari, Firefox, Edge, IE 11 browsers - Webpack and rollup.js module bundlers - - [React Native / Expo](#react-native-expo) + - [React Native / Expo](#react-native--expo) - **Secure** - Cryptographically-strong random values - **Small** - Zero-dependency, small footprint, plays nice with "tree shaking" packagers - **CLI** - Includes the [`uuid` command line](#command-line) utility @@ -252,7 +252,7 @@ uuidv4(v4options); // RESULT ### uuid.v5(name, namespace[, buffer[, offset]]) -Createa an RFC version 5 (namespace w/ SHA-1) UUID +Create an RFC version 5 (namespace w/ SHA-1) UUID | | | | --- | --- | @@ -302,6 +302,23 @@ uuidValidate('not a UUID'); // RESULT uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // RESULT ``` +Using `validate` and `version` together it is possible to do per-version validation, e.g. validate for only v4 UUIds. + +```javascript --run +import { version as uuidVersion } from 'uuid'; +import { validate as uuidValidate } from 'uuid'; + +function uuidValidateV4(uuid) { + return uuidValidate(uuid) && uuidVersion(uuid) === 4; +} + +const v1Uuid = 'd9428888-122b-11e1-b85c-61cd3cbb3210'; +const v4Uuid = '109156be-c4fb-41ea-b1b4-efe1671c5836'; + +uuidValidateV4(v4Uuid); // RESULT +uuidValidateV4(v1Uuid); // RESULT +``` + ### uuid.version(str) Detect RFC version of a UUID diff --git a/package-lock.json b/package-lock.json index 8cae0822..787509be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "uuid", - "version": "8.3.1", + "version": "8.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5c5ab2a4..f0ab3711 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid", - "version": "8.3.1", + "version": "8.3.2", "description": "RFC4122 (v1, v4, and v5) UUIDs", "commitlint": { "extends": [ diff --git a/src/rng-browser.js b/src/rng-browser.js index 1af1e4c9..38ed20ba 100644 --- a/src/rng-browser.js +++ b/src/rng-browser.js @@ -2,23 +2,27 @@ // require the crypto API and do not support built-in fallback to lower quality random number // generators (like Math.random()). -// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. Also, -// find the complete implementation of crypto (msCrypto) on IE11. -const getRandomValues = - (typeof crypto !== 'undefined' && - crypto.getRandomValues && - crypto.getRandomValues.bind(crypto)) || - (typeof msCrypto !== 'undefined' && - typeof msCrypto.getRandomValues === 'function' && - msCrypto.getRandomValues.bind(msCrypto)); +let getRandomValues; const rnds8 = new Uint8Array(16); export default function rng() { + // lazy load so that environments that need to polyfill have a chance to do so if (!getRandomValues) { - throw new Error( - 'crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported' - ); + // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. Also, + // find the complete implementation of crypto (msCrypto) on IE11. + getRandomValues = + (typeof crypto !== 'undefined' && + crypto.getRandomValues && + crypto.getRandomValues.bind(crypto)) || + (typeof msCrypto !== 'undefined' && + typeof msCrypto.getRandomValues === 'function' && + msCrypto.getRandomValues.bind(msCrypto)); + if (!getRandomValues) { + throw new Error( + 'crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported' + ); + } } return getRandomValues(rnds8);