Tuesday, October 18, 2022
HomeWeb DevelopmentUnderstanding UUIDs in Node.js - LogRocket Weblog

Understanding UUIDs in Node.js – LogRocket Weblog


Person id and safety are crucial components for constructing fashionable functions, and plenty of measures are in place to make sure the safety of customers’ identities.

One methodology of consumer identification on the web is UUIDs, or universally distinctive identifiers. Just like Microsoft’s GUIDs (globally distinctive identifiers), UUIDs are distinctive 128-bit values popularly used to uniquely determine entities on the web.

On this article, you’ll study UUIDs, UUID collisions, and the best way to generate UUIDs in Node.js utilizing three packages.

To leap forward:

How do UUIDs work?

The IETF (Web Engineering Activity Power) defines the UUID protocol in RFC 4122 as “A 128-bits-long identifier that may assure uniqueness throughout area and time.”

The era of various UUID variations happens with totally different algorithms and strategies. Whereas v1 UUIDs use the timestamp and Mac handle of the producing laptop to determine, v4 UUIDs use random quantity turbines relying on the web site that’s producing them.

Most working methods have a CLI device for producing UUIDs.

uuidgen             // generates a UUID
uuidgen assist        // view assist for the UUID command.

The uuidgen command is on the market on Home windows, Linux, and macOS methods to generate UUIDs on the command line or terminal.

You should utilize UUIDs in lots of areas of software program growth, from distributed functions, databases, networking, and situations the place the next diploma of randomness is important.

UUID size and collisions

As a result of the UUID protocol was designed to implement distinctive UUIDs, UUID collisions happen when two or extra computer systems generate the identical UUID.

Every UUID is distinct from different present UUIDs, with a 0.00000006 collision chance and an estimated 85 years earlier than the primary case of collision (when there will probably be 2.71 quintillion UUIDs) if computer systems generate one billion UUIDs per second.

UUID collisions could also be detrimental, primarily when utilized in the identical case — for instance, a UUID collision the place the UUIDs are the first keys in a database.

The usual size of generated UUIDs is 128 bits. Nevertheless, you possibly can shorten a UUID for numerous functions, though this isn’t suggested as a result of shortening UUIDs will increase the chance of collisions. In crucial circumstances, shortening UUIDs could also be detrimental to your software.

Producing UUIDs in Node.js

Most programming languages present functionalities for producing UUIDs. Within the Node.js runtime, the built-in crypto package deal has a randomUUID methodology for producing UUIDs.

First, import the crypto package deal into your JavaScript file.

const crypto = require('crypto');

Calling the UUID methodology returns a UUID of normal size that you should utilize in your program.

let uuid = crypto.randomUUID();
console.log(uuid);

The code prints the generated UUID to the console.

The Generated UUID Printed To The Console

Packages for producing UUIDs

There are lots of exterior npm packages for producing UUIDs. The packages present extra functionalities than you’ll discover within the crypto package deal.

Some standard npm packages for producing UUIDs in JavaScript are the uuid and short-uuid packages.

The uuid package deal

The uuid package deal gives performance for producing cryptographically safe customary UUIDs with assist for variations 1, 3, 4, and 5 UUIDs, in addition to cross-platform assist for Node.js, CommonJS, Webpack, React Native Expo, and extra.

The UUID package deal is an exterior dependency, so that you’ll have to put in the package deal.

npm set up uuid

After putting in the uuid package deal, import the package deal into your JavaScript file.


Extra nice articles from LogRocket:


const uuid = require('uuid');

Right here’s an instance of producing a v4 UUID with the uuid package deal.

const uuid4 = uuid.v4()
console.log(uuid4)

The v4 methodology returns v4 UUIDs, and the code prints the UUID to the console.

Version 4 UUIDs Printed To The Console

The short-uuid package deal

The short-uuid package deal gives functionalities for producing and translating RFC4122 v4-compliant customary UUIDs into shorter codecs and versa. You should utilize the short-uuid package deal to generate v4 UUIDs and shorten them in your software’s use circumstances.

The short-uuid package deal is safe, with options like errors on incorrect UUIDs. By default, the short-uuid package deal returns shortened IDs of a constant size, besides in the event you specify a size, and the package deal shortens UUIDs by padding the alphabet characters.

Run this command within the terminal of your working listing to put in the short-uuid package deal.

npm set up short-uuid

After putting in the short-uuid package deal, you possibly can import the package deal into your app.

const brief = require('short-uuid');

You’ll be able to generate a easy, lengthy UUID with the uuid methodology of your short-uuid occasion.

console.log(brief.uuid());

The uuid methodology returns a UUID of normal size 128 bits.

You’ll be able to generate shorter UUIDs with the generate methodology. The generate methodology returns a Flickr base58format by default.

base58 = brief.generate()
console.log(base58);

Generating Short UUIDs Using The Generate Method

You’ll be able to add further arguments to your short-uuid package deal occasion for additional functionalities. Including distinct values will yield padded codecs of the UUIDs.

// should not be duplicated
const translator = brief("32814"); // Present a selected alphabet for translation

const uuid = translator.generate()
console.log(uuid)

The operate will throw an error if there are duplicates within the alphabet values.

Right here’s how one can shorten UUIDs and retrieve the unique values with the short-uuid package deal.

const brief = require('short-uuid');

const translator = brief("342");

newUUID = translator.new()

authentic = translator.fromUUID(newUUID);

bits128 = translator.toUUID(authentic);

console.log(newUUID) // prints the generated UUID
console.log(authentic) // prints the unique UUID
console.log(bits128) // prints reverted UUID of 128bits

You generated a brand new UUID with the new methodology, an alias for the generate methodology that returns a padded UUID. The newUUID variable is the padded UUID generated by short-uuid, and the fromUUID methodology returns the unique 128-bit UUID earlier than padding. The toUUID methodology returns the initially generated UUID with the package deal.

The ToUUID Method Returns The Original UUID

Conclusion

On this article, we explored UUIDs, UUID collisions, why you shouldn’t use brief UUIDS, and the best way to generate UUIDs out of your command line and JavaScript apps with the crypto package deal, uuid package deal, and short-uuid package deal.

UUIDs are helpful for a variety of use circumstances, and as you write extra code, you’ll discover extra use circumstances for UUIDs in your apps.

200’s solely Monitor failed and gradual community requests in manufacturing

Deploying a Node-based internet app or web site is the straightforward half. Ensuring your Node occasion continues to serve sources to your app is the place issues get harder. In case you’re interested by making certain requests to the backend or third social gathering providers are profitable, attempt LogRocket. https://logrocket.com/signup/

LogRocket is sort of a DVR for internet and cell apps, recording actually all the pieces that occurs whereas a consumer interacts along with your app. As a substitute of guessing why issues occur, you possibly can combination and report on problematic community requests to shortly perceive the basis trigger.

LogRocket devices your app to report baseline efficiency timings comparable to web page load time, time to first byte, gradual community requests, and likewise logs Redux, NgRx, and Vuex actions/state. .

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments