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
- v3
- v4
- v5
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 linesThe following response is returned:
{
"v3": {
"uuid": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc",
"valid": true,
"version": 3,
"expectedV3": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc"
}
}
const {v4, validate, version} = require('uuid');
export default req => {
const uuid = v4();
return req.ok({
v4: uuid,
valid: validate(uuid),
version: version(uuid),
});
};
The following response is returned:
{
"v4": "ceae183a-8afa-42c4-9b8d-d9675bc6488c",
"valid": true,
"version": 4
}
const { v5, validate, version } = require('uuid');
export default req => {
const namespace = 'f8435596-ee6a-4a3d-a304-e3f0c7bb5321';
const identifier = 'Hello World';
const uuidV5 = v5(identifier, namespace);
const expectedV5 = '6e54de1b-b0b5-55df-97c7-5487107d155b';
return req.ok({
v5: {
uuid: uuidV5,
valid: validate(uuidV5),
version: version(uuidV5),
expectedV5: expectedV5,
show all 18 linesThe following response is returned:
{
"v5": {
"uuid": "6e54de1b-b0b5-55df-97c7-5487107d155b",
"valid": true,
"version": 5,
"expectedV5": "6e54de1b-b0b5-55df-97c7-5487107d155b"
}
}
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