UUID

UUID is a library for generating RFC-compliant UUIDs in JavaScript.

The uuid module is made available with the following require() statement:

const {v3, v4, v5, validate, version} = require('uuid');

Exposed constructors

After installing the library, you can use these constructors in PubNub Functions:

  • v3 — UUIDs generated based on the MD5 hash of a namespace identifier (like a URL or DNS name) and a name within that namespace.
  • v4 — UUIDs generated using random numbers. It is the simplest to use since it doesn’t require any inputs other than calling the method to generate a new UUID.
  • v5 — Similar to v3, but uses SHA-1 hashing instead of MD5.

Exposed methods

You can use these methods from the library directly in your Function's code:

  • validate to test a string and see if it is a valid UUID.
  • version to detect an RFC version of a UUID.

Examples

const { v3, validate, version } = require('uuid');

export default req => {
const namespace = 'f8435596-ee6a-4a3d-a304-e3f0c7bb5321';
const identifier = 'Hello World';
const uuidV3 = v3(identifier, namespace);

const expectedV3 = '51bfe3fe-6be8-3192-8ff2-b86ffd390fcc';

return req.ok({
v3: {
uuid: uuidV3,
valid: validate(uuidV3),
version: version(uuidV3),
expectedV3: expectedV3,
show all 18 lines

The following response is returned:

{
"v3": {
"uuid": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc",
"valid": true,
"version": 3,
"expectedV3": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc"
}
}
Functions support

Functions provides a rich set of tools, and this documentation does not cover all of the potential situations you may encounter. If you need help with a situation not covered by the documentation, please contact PubNub Support

Last updated on