npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apexsoftwaresolutions/uuidv4

v1.6.1

Published

UUIDv4 Generator Package

Downloads

4

Readme

UUIDv4

The UUIDv4 specification requires that the UUIDv4 value is a 128-bit number represented by a string of hexadecimal digits, which means that we need to generate a 16-byte buffer to store the random bits that make up the UUIDv4 value.

A UUIDv4 value is generated by selecting 128 bits at random, which are then formatted as a string of hexadecimal digits with the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where x is any hexadecimal digit and y is either 8, 9, A, or B.

To generate the random bits, we use the crypto.randomBytes method from Node's built-in crypto module, which generates a secure, random byte sequence of the specified length. In this case, we need 16 bytes, or 128 bits, to generate a valid UUIDv4 value.

Once we have the 16-byte buffer, we can then set the version number and variant number bits as required by the UUIDv4 specification and convert the buffer to a string of hexadecimal digits with the correct format.

  // Set the version number to 4 (0100)
  buffer[6] = (buffer[6] & 0x0f) | 0x40;

This line of code sets the version number bits of the UUIDv4 value in the 7th octet of the 16-byte buffer.

According to the UUIDv4 specification, the version number bits of a UUIDv4 value are defined as follows:

  • The four most significant bits of the 7th octet are set to 0100 (binary) or 4 (hexadecimal) to indicate a version number of 4. To set the version number bits in the buffer, we use a bit mask to clear the four most significant bits of the 7th octet and then set them to 0100 (binary) or 4 (hexadecimal).

The line of code you asked about achieves this by performing the following steps:

  • buffer[6] & 0x0f clears the four most significant bits of the 7th octet by performing a bitwise AND operation with the hexadecimal value 0x0f, which has the binary value 00001111.
  • | 0x40 sets the four most significant bits of the 7th octet to 0100 (binary) or 4 (hexadecimal) by performing a bitwise OR operation with the hexadecimal value 0x40, which has the binary value 01000000.

By setting the version number bits in the buffer, we ensure that the generated UUIDv4 value has a version number of 4, as required by the UUIDv4 specification.


  // Set the variant number to 2 (10)
  buffer[8] = (buffer[8] & 0x3f) | 0x80;

This line of code sets the variant number bits of the UUIDv4 value in the 9th octet of the 16-byte buffer.

According to the UUIDv4 specification, the variant number bits of a UUIDv4 value are defined as follows:

  • The two most significant bits of the 9th octet are set to 10 (binary) or 8 or 9 or A or B (hexadecimal) to indicate a variant number of 10x, where x can be any value.

To set the variant number bits in the buffer, we use a bit mask to clear the two most significant bits of the 9th octet and then set them to 10 (binary) or 8 or 9 or A or B (hexadecimal). The line of code you asked about achieves this by performing the following steps:

  • buffer[8] & 0x3f clears the two most significant bits of the 9th octet by performing a bitwise AND operation with the hexadecimal value 0x3f, which has the binary value 00111111.
  • | 0x80 sets the two most significant bits of the 9th octet to 10 (binary) or 8 or 9 or A or B (hexadecimal) by performing a bitwise OR operation with the hexadecimal value 0x80, which has the binary value 10000000.

By setting the variant number bits in the buffer, we ensure that the generated UUIDv4 value has a variant number of 10x, where x can be any value, as required by the UUIDv4 specification.


const uuid = buffer.toString("hex").toLowerCase();

This line of code converts the 16-byte buffer, which contains the random bits that make up the UUIDv4 value, to a string of hexadecimal digits with the correct format.

The buffer.toString("hex") method converts the buffer to a hexadecimal string representation. The "hex" argument specifies the encoding to use when converting the buffer to a string.

The toLowerCase() method converts all the alphabetic characters in the hexadecimal string to lowercase, as required by the UUIDv4 specification.

By converting the buffer to a string of hexadecimal digits with the correct format, we obtain the final UUIDv4 value, which can be used in a variety of contexts.


return `${uuid.substring(0, 8)}-${uuid.substring(8, 12)}-${uuid.substring(
    12,
    16
  )}-${uuid.substring(16, 20)}-${uuid.substring(20)}`

This line of code formats the final UUIDv4 value as a string with the correct format of xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx.

The substring method is used to extract the different parts of the UUIDv4 string from the uuid string. Each call to substring extracts a specific part of the UUIDv4 string based on its position within the string.

The following code:

${uuid.substring(0, 8)}-${uuid.substring(8, 12)}-${uuid.substring(12, 16)}-${uuid.substring(16, 20)}-${uuid.substring(20)}

extracts the five parts of the UUIDv4 string and separates them with hyphens to create the final formatted UUIDv4 string. Here's what each part corresponds to:

  • uuid.substring(0, 8): the first eight characters of the UUIDv4 string.
  • uuid.substring(8, 12): the next four characters of the UUIDv4 string.
  • uuid.substring(12, 16): the next four characters of the UUIDv4 string.
  • uuid.substring(16, 20): the next four characters of the UUIDv4 string.
  • uuid.substring(20): the final 12 characters of the UUIDv4 string.

By extracting and concatenating these parts in the correct order with hyphens, we obtain the final UUIDv4 string with the correct format of xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx.