Giter VIP home page Giter VIP logo

pure-uuid's Introduction

Pure-UUID

Pure JavaScript Based Universally Unique Identifier (UUID)

Abstract

This is a pure JavaScript and dependency-free library for the generation of DCE 1.1, ISO/IEC 11578:1996 and IETF RFC-4122 compliant Universally Unique Identifier (UUID). It supports DCE 1.1 variant UUIDs of version 1 (time and node based), version 3 (name based, MD5), version 4 (random number based) and version 5 (name based, SHA-1). It can be used in both Node.js based server and browser based client environments.

The essential points of this implementation (in contrast to the many others) are: First, although internally 32/64 bit unsigned integer arithmentic and MD5/SHA-1 digest algorithmic is required, this UUID implementation is fully self-contained and dependency-free. Second, this implementation wraps around either Uint8Array, Buffer or Array standard classes and this way tries to represent UUIDs as best as possible in the particular environment. Third, thanks to a Universal Module Definition (UMD) wrapper, this library works out-of-the-box in all important JavaScript run-time environments.

What is a UUID?

UUIDs are 128 bit numbers which are intended to have a high likelihood of uniqueness over space and time and are computationally difficult to guess. They are globally unique identifiers which can be locally generated without contacting a global registration authority. UUIDs are intended as unique identifiers for both mass tagging objects with an extremely short lifetime and to reliably identifying very persistent objects across a network.

UUID Binary Representation

According to the DCE 1.1, ISO/IEC 11578:1996 and IETF RFC-4122 standards, a DCE 1.1 variant UUID is a 128 bit number defined out of 7 fields, each field a multiple of an octet in size and stored in network byte order:

                                                    [4]
                                                   version
                                                 -->|  |<--
                                                    |  |
                                                    |  |  [16]
                [32]                      [16]      |  |time_hi
              time_low                  time_mid    | _and_version
    |<---------------------------->||<------------>||<------------>|
    | MSB                          ||              ||  |           |
    | /                            ||              ||  |           |
    |/                             ||              ||  |           |

    +------++------++------++------++------++------++------++------+~~
    |  15  ||  14  ||  13  ||  12  ||  11  ||  10  |####9  ||   8  |
    | MSO  ||      ||      ||      ||      ||      |####   ||      |
    +------++------++------++------++------++------++------++------+~~
    7654321076543210765432107654321076543210765432107654321076543210

  ~~+------++------++------++------++------++------++------++------+
    ##* 7  ||   6  ||   5  ||   4  ||   3  ||   2  ||   1  ||   0  |
    ##*    ||      ||      ||      ||      ||      ||      ||  LSO |
  ~~+------++------++------++------++------++------++------++------+
    7654321076543210765432107654321076543210765432107654321076543210

    | |    ||      ||                                             /|
    | |    ||      ||                                            / |
    | |    ||      ||                                          LSB |
    |<---->||<---->||<-------------------------------------------->|
    |clk_seq clk_seq                      node
    |_hi_res _low                         [48]
    |[5-6]    [8]
    | |
 -->| |<--
  variant
   [2-3]

An example of a UUID binary representation is the octet stream 0xF8 0x1D 0x4F 0xAE 0x7D 0xEC 0x11 0xD0 0xA7 0x65 0x00 0xA0 0xC9 0x1E 0x6B 0xF6.

UUID ASCII String Representation

According to the DCE 1.1, ISO/IEC 11578:1996 and IETF RFC-4122 standards, a DCE 1.1 variant UUID is represented as an ASCII string consisting of 8 hexadecimal digits followed by a hyphen, then three groups of 4 hexadecimal digits each followed by a hyphen, then 12 hexadecimal digits.

Getting Pure-UUID

$ npm install pure-uuid

Using Pure-UUID

  • global environment:
var uuid = new UUID(3, "ns:URL", "http://example.com/");
  • CommonJS environment:
var UUID = require("pure-uuid");
var uuid = new UUID(3, "ns:URL", "http://example.com/");
  • AMD environment:
define(["pure-uuid"], function (UUID) {
    var uuid = new UUID(3, "ns:URL", "http://example.com/");
});

API

interface UUID {
    /*  making  */
    make(version: number, ...params: any[]): UUID;

    /*  parsing  */
    parse(str: string): UUID;

    /*  formatting  */
    format(type?: string): string;

    /*  formatting (alias)  */
    toString(type?: string): string;

    /*  sensible JSON serialization  */
    toJSON(): string;

    /*  importing  */
    import(arr: number[]): UUID;

    /*  exporting  */
    export(): number[];

    /*  byte-wise comparison  */
    compare(other: UUID): boolean;

    /*  equal check  */
    equal(other: UUID): boolean;

    /*  fold 1-4 times  */
    fold(k: number): number[];
}

export interface UUIDConstructor {
  /*  default construction  */
  new(): UUID;

  /*  parsing construction  */
  new(uuid: string): UUID;

  /*  making construction  */
  new(version: number): UUID;
  new(version: number, ns: string, data: string): UUID;
}

declare var UUID: UUIDConstructor;
export default UUID;

Examples

//  load a UUID
uuid = new UUID("0a300ee9-f9e4-5697-a51a-efc7fafaba67");

//  make a UUID version 1 (time and node based)
uuid = new UUID(1);

//  make a UUID version 3 (name-based, MD5)
uuid = new UUID(3, "ns:URL", "http://example.com/");

//  make a UUID version 4 (random number based)
uuid = new UUID(4);

//  make a UUID version 5 (name-based, SHA-1)
uuid = new UUID(5, "ns:URL", "http://example.com/");

//  format a UUID in standard format
str = uuid.format()
str = uuid.format("std")

//  format a UUID in Base16 format
str = uuid.format("b16")

//  format a UUID in ZeroMQ-Base85 format
str = uuid.format("z85")

License

Copyright © 2004-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pure-uuid's People

Contributors

bennycode avatar frasern avatar mixedcase avatar rmalecky avatar rse avatar seblm avatar xaviergxf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pure-uuid's Issues

AMD define should produce anonymous module

Hi,
The UMD mechanism employed sets the AMD module name to "UUID", which prevents usage of pure-uuid as an anonymous module. I believe what you really want is to omit the module name, and either replace it with [] to signify no dependencies or to skip that argument altogether. Once I made that change locally, it worked fine.

Typo: UInt8Array

When setting up the prototype, the code checks if UInt8Array is defined.

This looks like a typo; it should instead be checking for Uint8Array (lowercase i).

Code "will not work in any environment"

Firstly thank you for doing all this hard work to make using uuids easy to generate. Very much appreciated!

I just tried to use Version 1.6.2 of your code in a NetSuite environment which is a essentially a Mozilla javascript engine written in Java that supports only very old version of javascript (well before the advent of modern Node). As such the buffer type doesn't exist.

This is fine since it can be polyfilled, however I am having an issue where this error is raised on line 701 of the uuid.js file.

org.mozilla.javascript.EcmaError: ReferenceError: "Buffer" is not defined.

Perhaps in a future version rather than a simple "if (Buffer)" you could implement a safe method like the typeof line above so that the code will fall through to the else clause and still have a chance of working?!

Otherwise great job!

Thanks.

Paolo
SoftWUD

Please add support of ESModule

Hi, I'm trying to use this library on Deno, but Deno is only support ESModuels now, CommonJS Modules is abandoned. And even if not for Deno, an ESModule will be great for other projects, cause it can reduce some step to transform CommonJS Modules to ESModules.

Please add support, thanks.

UUID is not exported as default

In the TypeScript definition, UUID is marked as the default export. However, the UMD file does not provide it as the default, so the ES6+ import statement (import UUID from 'pure-uuid') fails despite passing TS checking. Instead, the import statement has to aim a bit wider (import * as UUID from 'pure-uuid'`). Can either the definition be corrected or the module export a default property aliasing the actual export so that both the TS definition and actual export mesh?

export default UUID;

toString() method

I like "pure-uuid" (because it's available on Bower & npm) but it would be great if there would be a toString method to receive a new UUID(4) also as string and not just as typed array.

Typescript compare doesnt returns boolean

Hi,

The typescript definition uuid.d.ts on function compare returns a boolean, but in reality it returns a number. When using this on a filter it inverses the desired results. Can you please fix this issue? How about creating a more specific method like equal which returns a boolean?

Thanks

non-unique v5 IDs are generated

using the following invocation, non-unique IDs are generated:

let a = new UUID(5, "ns:URL", "http://example.com/").format();
let b = new UUID(5, "ns:URL", "http://example.com/").format();
let c = new UUID(5, "ns:URL", "http://example.com/").format();

console.log({a,b,c})

Expected behaviour: The values should be unique

Issue when using in typescript project

The latest version (1.8.0) throws the following error when used in a typescript project. The previous version (1.7.0) works fine.

node_modules/pure-uuid/uuid.d.ts:70:1 - error TS2309: An export assignment cannot be used in a module with other exported elements.

70 export = UUID;
   ~~~~~~~~~~~~~~

Duplicate UUIDs Generated Using new UUID(4)

Issue:
We've observed that the new UUID(4) function from the pure-uuid npm package has returned identical UUIDs in a few instances, even though these instances were months apart.

Details:
Package Name: pure-uuid
Function in Question: new UUID(4)
Observed Behavior: Duplicate UUIDs generated in separate instances months apart.
Expected Behavior: UUIDs, especially v4, should be universally unique and the chances of collisions should be extremely low.

Steps to Reproduce:
This issue might not be consistently reproducible given the nature of UUIDs, but it has been observed in our environment multiple times.

Request:
We kindly request the maintainers look into this issue and provide a fix or clarification on the observed behavior. If there are any best practices or additional configurations that we might have missed, please advise.

Thank you for your attention to this matter.

Malformed UUIDs v4

I have been using new UUID(4).format() and noticed that it generates some invalid / malformed UUIDs.

A proper UUID v4 looks like this:

  • b277311c-dd9c-45c1-a041-2f78c5fd066e (32 chars total, 4 dashes)

But I somehow received the following UUIDs:

  • 298f8a6b-3e-1c4-4bba-9488-8f4b233e63e7 (34 chars total, 5 dashes)
  • 15ee-168da--20c6-4c1f0-9a2b--2806bf323e706 (36 chars total, 6 dashes)

Is it possible that UUID(4).format() generates UUIDs with too many dashes (with 1 extra char per extra dash)? Can this library be affected by a sign problem?

Namespace check is too strict?

The new UUID().parse(arguments[1]) phase only accepts your pre-defined few types. This is not documented if I'm not mistaken. Why has this logic been added since 1.1.x?

This UUID(3, "whatever", "value") used to work, now it fails.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.