1 | # uuid [![Build Status](https://secure.travis-ci.org/kelektiv/node-uuid.svg?branch=master)](http://travis-ci.org/kelektiv/node-uuid) #
|
2 |
|
3 | Simple, fast generation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDS.
|
4 |
|
5 | Features:
|
6 |
|
7 | * Support for version 1, 4 and 5 UUIDs
|
8 | * Cross-platform
|
9 | * Uses cryptographically-strong random number APIs (when available)
|
10 | * Zero-dependency, small footprint (... but not [this small](https://gist.github.com/982883))
|
11 |
|
12 | ## Quickstart - CommonJS (Recommended)
|
13 |
|
14 | ```shell
|
15 | npm install uuid
|
16 | ```
|
17 |
|
18 | Then generate your uuid version of choice ...
|
19 |
|
20 | Version 1 (timestamp):
|
21 |
|
22 | ```javascript
|
23 | const uuidv1 = require('uuid/v1');
|
24 | uuidv1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
|
25 | ```
|
26 |
|
27 | Version 4 (random):
|
28 |
|
29 | ```javascript
|
30 | const uuidv4 = require('uuid/v4');
|
31 | uuidv4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'
|
32 | ```
|
33 |
|
34 | Version 5 (namespace):
|
35 |
|
36 | ```javascript
|
37 | const uuidv5 = require('uuid/v5');
|
38 |
|
39 | // ... using predefined DNS namespace (for domain names)
|
40 | uuidv5('hello.example.com', uuidv5.DNS)); // -> 'fdda765f-fc57-5604-a269-52a7df8164ec'
|
41 |
|
42 | // ... using predefined URL namespace (for, well, URLs)
|
43 | uuidv5('http://example.com/hello', uuidv5.URL); // -> '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
|
44 |
|
45 | // ... using a custom namespace
|
46 | const MY_NAMESPACE = '<UUID string you previously generated elsewhere>';
|
47 | uuidv5('Hello, World!', MY_NAMESPACE); // -> '90123e1c-7512-523e-bb28-76fab9f2f73d'
|
48 | ```
|
49 |
|
50 | ## Quickstart - Browser-ready Versions
|
51 |
|
52 | Browser-ready versions of this module are available via [wzrd.in](https://github.com/jfhbrook/wzrd.in).
|
53 |
|
54 | For version 1 uuids:
|
55 |
|
56 | ```html
|
57 | <script src="http://wzrd.in/standalone/uuid%2Fv1@latest"></script>
|
58 | <script>
|
59 | uuidv1(); // -> v1 UUID
|
60 | </script>
|
61 | ```
|
62 |
|
63 | For version 4 uuids:
|
64 |
|
65 | ```html
|
66 | <script src="http://wzrd.in/standalone/uuid%2Fv4@latest"></script>
|
67 | <script>
|
68 | uuidv4(); // -> v4 UUID
|
69 | </script>
|
70 | ```
|
71 |
|
72 | For version 5 uuids:
|
73 |
|
74 | ```html
|
75 | <script src="http://wzrd.in/standalone/uuid%2Fv5@latest"></script>
|
76 | <script>
|
77 | uuidv5('http://example.com/hello', uuidv5.URL); // -> v5 UUID
|
78 | </script>
|
79 | ```
|
80 |
|
81 | ## API
|
82 |
|
83 | ### Version 1
|
84 |
|
85 | ```javascript
|
86 | const uuidv1 = require('uuid/v1');
|
87 |
|
88 | // Allowed arguments
|
89 | uuidv1();
|
90 | uuidv1(options);
|
91 | uuidv1(options, buffer, offset);
|
92 | ```
|
93 |
|
94 | Generate and return a RFC4122 v1 (timestamp-based) UUID.
|
95 |
|
96 | * `options` - (Object) Optional uuid state to apply. Properties may include:
|
97 |
|
98 | * `node` - (Array) Node id as Array of 6 bytes (per 4.1.6). Default: Randomly generated ID. See note 1.
|
99 | * `clockseq` - (Number between 0 - 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
|
100 | * `msecs` - (Number | Date) Time in milliseconds since unix Epoch. Default: The current time is used.
|
101 | * `nsecs` - (Number between 0-9999) additional time, in 100-nanosecond units. Ignored if `msecs` is unspecified. Default: internal uuid counter is used, as per 4.2.1.2.
|
102 |
|
103 | * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
104 | * `offset` - (Number) Starting index in `buffer` at which to begin writing.
|
105 |
|
106 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
107 |
|
108 | Note: The <node> id is generated guaranteed to stay constant for the lifetime of the current JS runtime. (Future versions of this module may use persistent storage mechanisms to extend this guarantee.)
|
109 |
|
110 | Example: Generate string UUID with fully-specified options
|
111 |
|
112 | ```javascript
|
113 | uuidv1({
|
114 | node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
|
115 | clockseq: 0x1234,
|
116 | msecs: new Date('2011-11-01').getTime(),
|
117 | nsecs: 5678
|
118 | }); // -> "710b962e-041c-11e1-9234-0123456789ab"
|
119 | ```
|
120 |
|
121 | Example: In-place generation of two binary IDs
|
122 |
|
123 | ```javascript
|
124 | // Generate two ids in an array
|
125 | const arr = new Array(32); // -> []
|
126 | uuidv1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
|
127 | uuidv1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
|
128 | ```
|
129 |
|
130 | ### Version 4
|
131 |
|
132 | ```javascript
|
133 | const uuidv4 = require('uuid/v4')
|
134 |
|
135 | // Allowed arguments
|
136 | uuidv4();
|
137 | uuidv4(options);
|
138 | uuidv4(options, buffer, offset);
|
139 | ```
|
140 |
|
141 | Generate and return a RFC4122 v4 UUID.
|
142 |
|
143 | * `options` - (Object) Optional uuid state to apply. Properties may include:
|
144 | * `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values
|
145 | * `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255)
|
146 | * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
147 | * `offset` - (Number) Starting index in `buffer` at which to begin writing.
|
148 |
|
149 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
150 |
|
151 | Example: Generate string UUID with fully-specified options
|
152 |
|
153 | ```javascript
|
154 | uuid.v4({
|
155 | random: [
|
156 | 0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea,
|
157 | 0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36
|
158 | ]
|
159 | });
|
160 | // -> "109156be-c4fb-41ea-b1b4-efe1671c5836"
|
161 | ```
|
162 |
|
163 | Example: Generate two IDs in a single buffer
|
164 |
|
165 | ```javascript
|
166 | const buffer = new Array(32); // (or 'new Buffer' in node.js)
|
167 | uuid.v4(null, buffer, 0);
|
168 | uuid.v4(null, buffer, 16);
|
169 | ```
|
170 |
|
171 | ### Version 5
|
172 |
|
173 | ```javascript
|
174 | const uuidv5 = require('uuid/v4');
|
175 |
|
176 | // Allowed arguments
|
177 | uuidv5(name, namespace);
|
178 | uuidv5(name, namespace, buffer);
|
179 | uuidv5(name, namespace, buffer, offset);
|
180 | ```
|
181 |
|
182 | Generate and return a RFC4122 v4 UUID.
|
183 |
|
184 | * `name` - (String | Array[]) "name" to create UUID with
|
185 | * `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
|
186 | * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
187 | * `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
|
188 |
|
189 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
190 |
|
191 | Example:
|
192 |
|
193 | ```javascript
|
194 | // Generate a unique namespace (typically you would do this once, outside of
|
195 | // your project, then bake this value into your code)
|
196 | const uuidv4 = require('uuid/v4');
|
197 | const MY_NAMESPACE = uuidv4(); //
|
198 |
|
199 | // Generate a couple namespace uuids
|
200 | const uuidv5 = require('uuid/v5');
|
201 | uuidv5('hello', MY_NAMESPACE);
|
202 | uuidv5('world', MY_NAMESPACE);
|
203 | ```
|
204 |
|
205 | ## Testing
|
206 |
|
207 | ```shell
|
208 | npm test
|
209 | ```
|
210 |
|
211 | ## Deprecated / Browser-ready API
|
212 |
|
213 | The API below is available for legacy purposes and is not expected to be available post-3.X
|
214 |
|
215 | ```javascript
|
216 | const uuid = require('uuid');
|
217 |
|
218 | uuid.v1(...); // alias of uuid/v1
|
219 | uuid.v4(...); // alias of uuid/v4
|
220 | uuid(...); // alias of uuid/v4
|
221 |
|
222 | // uuid.v5() is not supported in this API
|
223 | ```
|
224 |
|
225 | ## Legacy node-uuid package
|
226 |
|
227 | The code for the legacy node-uuid package is available in the `node-uuid` branch.
|