Debian Codegraph @

node/uuid/README.md

raw
1 <!--
2 -- This file is auto-generated from README_js.md. Changes should be made there.
3 -->
4
5 # uuid [![CI](https://github.com/uuidjs/uuid/workflows/CI/badge.svg)](https://github.com/uuidjs/uuid/actions?query=workflow%3ACI) [![Browser](https://github.com/uuidjs/uuid/workflows/Browser/badge.svg)](https://github.com/uuidjs/uuid/actions/workflows/browser.yml)
6
7 For the creation of [RFC9562](https://www.rfc-editor.org/rfc/rfc9562.html) (formerly [RFC4122](https://www.rfc-editor.org/rfc/rfc4122.html)) UUIDs
8
9 - **Complete** - Support for all RFC9562 UUID versions
10 - **Cross-platform** - Support for...
11 - [Typescript](#support)
12 - [Chrome, Safari, Firefox, and Edge](#support)
13 - [NodeJS](#support)
14 - [React Native / Expo](#react-native--expo)
15 - **Secure** - Uses modern `crypto` API for random values
16 - **Compact** - Zero-dependency, [tree-shakable](https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking)
17 - **CLI** - [`uuid` command line](#command-line) utility
18
19 <!-- prettier-ignore -->
20 > [!NOTE]
21 >
22 > Starting with `uuid@12` CommonJS is no longer supported. See [implications](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) and [motivation](https://github.com/uuidjs/uuid/issues/881) for details.
23
24 ## Quickstart
25
26 **1. Install**
27
28 ```shell
29 npm install uuid
30 ```
31
32 **2. Create a UUID**
33
34 ```javascript
35 import { v4 as uuidv4 } from 'uuid';
36
37 uuidv4(); // ⇨ 'b18794e8-5d0d-417c-b361-ba38e78411b4'
38 ```
39
40 For timestamp UUIDs, namespace UUIDs, and other options read on ...
41
42 ## API Summary
43
44 | | |
45 | --- | --- |
46 | [`uuid.NIL`](#uuidnil) | The nil UUID string (all zeros) |
47 | [`uuid.MAX`](#uuidmax) | The max UUID string (all ones) |
48 | [`uuid.parse()`](#uuidparsestr) | Convert UUID string to array of bytes |
49 | [`uuid.stringify()`](#uuidstringifyarr-offset) | Convert array of bytes to UUID string |
50 | [`uuid.v1()`](#uuidv1options-buffer-offset) | Generate a version 1 (timestamp) UUID |
51 | [`uuid.v1ToV6()`](#uuidv1tov6uuid) | Convert a version 1 UUID to version 6 |
52 | [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Generate a version 3 (namespace w/ MD5) UUID |
53 | [`uuid.v4()`](#uuidv4options-buffer-offset) | Generate a version 4 (random) UUID |
54 | [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Generate a version 5 (namespace w/ SHA-1) UUID |
55 | [`uuid.v6()`](#uuidv6options-buffer-offset) | Generate a version 6 (timestamp, reordered) UUID |
56 | [`uuid.v6ToV1()`](#uuidv6tov1uuid) | Convert a version 6 UUID to version 1 |
57 | [`uuid.v7()`](#uuidv7options-buffer-offset) | Generate a version 7 (Unix Epoch time-based) UUID |
58 | ~~[`uuid.v8()`](#uuidv8)~~ | "Intentionally left blank" |
59 | [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID |
60 | [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID |
61
62 ## API
63
64 ### uuid.NIL
65
66 The nil UUID string (all zeros).
67
68 Example:
69
70 ```javascript
71 import { NIL as NIL_UUID } from 'uuid';
72
73 NIL_UUID; // ⇨ '00000000-0000-0000-0000-000000000000'
74 ```
75
76 ### uuid.MAX
77
78 The max UUID string (all ones).
79
80 Example:
81
82 ```javascript
83 import { MAX as MAX_UUID } from 'uuid';
84
85 MAX_UUID; // ⇨ 'ffffffff-ffff-ffff-ffff-ffffffffffff'
86 ```
87
88 ### uuid.parse(str)
89
90 Convert UUID string to array of bytes
91
92 | | |
93 | --------- | ---------------------------------------- |
94 | `str` | A valid UUID `String` |
95 | _returns_ | `Uint8Array[16]` |
96 | _throws_ | `TypeError` if `str` is not a valid UUID |
97
98 <!-- prettier-ignore -->
99 > [!NOTE]
100 > Ordering of values in the byte arrays used by `parse()` and `stringify()` follows the left &Rarr; right order of hex-pairs in UUID strings. As shown in the example below.
101
102 Example:
103
104 ```javascript
105 import { parse as uuidParse } from 'uuid';
106
107 // Parse a UUID
108 uuidParse('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨
109 // Uint8Array(16) [
110 // 110, 192, 189, 127, 17,
111 // 192, 67, 218, 151, 94,
112 // 42, 138, 217, 235, 174,
113 // 11
114 // ]
115 ```
116
117 ### uuid.stringify(arr[, offset])
118
119 Convert array of bytes to UUID string
120
121 | | |
122 | -------------- | ---------------------------------------------------------------------------- |
123 | `arr` | `Array`-like collection of 16 values (starting from `offset`) between 0-255. |
124 | [`offset` = 0] | `Number` Starting index in the Array |
125 | _returns_ | `String` |
126 | _throws_ | `TypeError` if a valid UUID string cannot be generated |
127
128 <!-- prettier-ignore -->
129 > [!NOTE]
130 > Ordering of values in the byte arrays used by `parse()` and `stringify()` follows the left &Rarr; right order of hex-pairs in UUID strings. As shown in the example below.
131
132 Example:
133
134 ```javascript
135 import { stringify as uuidStringify } from 'uuid';
136
137 const uuidBytes = Uint8Array.of(
138 0x6e,
139 0xc0,
140 0xbd,
141 0x7f,
142 0x11,
143 0xc0,
144 0x43,
145 0xda,
146 0x97,
147 0x5e,
148 0x2a,
149 0x8a,
150 0xd9,
151 0xeb,
152 0xae,
153 0x0b
154 );
155
156 uuidStringify(uuidBytes); // ⇨ '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'
157 ```
158
159 ### uuid.v1([options[, buffer[, offset]]])
160
161 Create an RFC version 1 (timestamp) UUID
162
163 | | |
164 | --- | --- |
165 | [`options`] | `Object` with one or more of the following properties: |
166 | [`options.node = (random)` ] | RFC "node" field as an `Array[6]` of byte values (per 4.1.6) |
167 | [`options.clockseq = (random)`] | RFC "clock sequence" as a `Number` between 0 - 0x3fff |
168 | [`options.msecs = (current time)`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
169 | [`options.nsecs = 0`] | RFC "timestamp" field (`Number` of nanoseconds to add to `msecs`, should be 0-10,000) |
170 | [`options.random = (random)`] | `Array` of 16 random bytes (0-255) used to generate other fields, above |
171 | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
172 | [`buffer`] | `Uint8Array` or `Uint8Array` subtype (e.g. Node.js `Buffer`). If provided, binary UUID is written into the array, starting at `offset` |
173 | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
174 | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
175 | _throws_ | `Error` if more than 10M UUIDs/sec are requested |
176
177 Example:
178
179 ```javascript
180 import { v1 as uuidv1 } from 'uuid';
181
182 uuidv1(); // ⇨ '57fd0000-c7d3-11ef-841d-514d2167fc5b'
183 ```
184
185 Example using `options`:
186
187 ```javascript
188 import { v1 as uuidv1 } from 'uuid';
189
190 const options = {
191 node: Uint8Array.of(0x01, 0x23, 0x45, 0x67, 0x89, 0xab),
192 clockseq: 0x1234,
193 msecs: new Date('2011-11-01').getTime(),
194 nsecs: 5678,
195 };
196 uuidv1(options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
197 ```
198
199 ### uuid.v1ToV6(uuid)
200
201 Convert a UUID from version 1 to version 6
202
203 ```javascript
204 import { v1ToV6 } from 'uuid';
205
206 v1ToV6('92f62d9e-22c4-11ef-97e9-325096b39f47'); // ⇨ '1ef22c49-2f62-6d9e-97e9-325096b39f47'
207 ```
208
209 ### uuid.v3(name, namespace[, buffer[, offset]])
210
211 Create an RFC version 3 (namespace w/ MD5) UUID
212
213 API is identical to `v5()`, but uses "v3" instead.
214
215 <!-- prettier-ignore -->
216 > [!IMPORTANT]
217 > Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
218
219 ### uuid.v4([options[, buffer[, offset]]])
220
221 Create an RFC version 4 (random) UUID
222
223 | | |
224 | --- | --- |
225 | [`options`] | `Object` with one or more of the following properties: |
226 | [`options.random`] | `Array` of 16 random bytes (0-255) |
227 | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
228 | [`buffer`] | `Uint8Array` or `Uint8Array` subtype (e.g. Node.js `Buffer`). If provided, binary UUID is written into the array, starting at `offset` |
229 | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
230 | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
231
232 Example:
233
234 ```javascript
235 import { v4 as uuidv4 } from 'uuid';
236
237 uuidv4(); // ⇨ 'b18794e8-5d0d-417c-b361-ba38e78411b4'
238 ```
239
240 Example using predefined `random` values:
241
242 ```javascript
243 import { v4 as uuidv4 } from 'uuid';
244
245 const v4options = {
246 random: Uint8Array.of(
247 0x10,
248 0x91,
249 0x56,
250 0xbe,
251 0xc4,
252 0xfb,
253 0xc1,
254 0xea,
255 0x71,
256 0xb4,
257 0xef,
258 0xe1,
259 0x67,
260 0x1c,
261 0x58,
262 0x36
263 ),
264 };
265 uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
266 ```
267
268 ### uuid.v5(name, namespace[, buffer[, offset]])
269
270 Create an RFC version 5 (namespace w/ SHA-1) UUID
271
272 | | |
273 | --- | --- |
274 | `name` | `String \| Array` |
275 | `namespace` | `String \| Array[16]` Namespace UUID |
276 | [`buffer`] | `Uint8Array` or `Uint8Array` subtype (e.g. Node.js `Buffer`). If provided, binary UUID is written into the array, starting at `offset` |
277 | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
278 | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
279
280 <!-- prettier-ignore -->
281 > [!NOTE]
282 > The RFC `DNS` and `URL` namespaces are available as `v5.DNS` and `v5.URL`.
283
284 Example with custom namespace:
285
286 ```javascript
287 import { v5 as uuidv5 } from 'uuid';
288
289 // Define a custom namespace. Readers, create your own using something like
290 // https://www.uuidgenerator.net/
291 const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
292
293 uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
294 ```
295
296 Example with RFC `URL` namespace:
297
298 ```javascript
299 import { v5 as uuidv5 } from 'uuid';
300
301 uuidv5('https://www.w3.org/', uuidv5.URL); // ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'
302 ```
303
304 ### uuid.v6([options[, buffer[, offset]]])
305
306 Create an RFC version 6 (timestamp, reordered) UUID
307
308 This method takes the same arguments as uuid.v1().
309
310 ```javascript
311 import { v6 as uuidv6 } from 'uuid';
312
313 uuidv6(); // ⇨ '1efc7d35-7fd0-6000-841d-504d2167fc5b'
314 ```
315
316 Example using `options`:
317
318 ```javascript
319 import { v6 as uuidv6 } from 'uuid';
320
321 const options = {
322 node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
323 clockseq: 0x1234,
324 msecs: new Date('2011-11-01').getTime(),
325 nsecs: 5678,
326 };
327 uuidv6(options); // ⇨ '1e1041c7-10b9-662e-9234-0123456789ab'
328 ```
329
330 ### uuid.v6ToV1(uuid)
331
332 Convert a UUID from version 6 to version 1
333
334 ```javascript
335 import { v6ToV1 } from 'uuid';
336
337 v6ToV1('1ef22c49-2f62-6d9e-97e9-325096b39f47'); // ⇨ '92f62d9e-22c4-11ef-97e9-325096b39f47'
338 ```
339
340 ### uuid.v7([options[, buffer[, offset]]])
341
342 Create an RFC version 7 (random) UUID
343
344 | | |
345 | --- | --- |
346 | [`options`] | `Object` with one or more of the following properties: |
347 | [`options.msecs = (current time)`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
348 | [`options.random = (random)`] | `Array` of 16 random bytes (0-255) used to generate other fields, above |
349 | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
350 | [`options.seq = (random)`] | 32-bit sequence `Number` between 0 - 0xffffffff. This may be provided to help ensure uniqueness for UUIDs generated within the same millisecond time interval. Default = random value. |
351 | [`buffer`] | `Uint8Array` or `Uint8Array` subtype (e.g. Node.js `Buffer`). If provided, binary UUID is written into the array, starting at `offset` |
352 | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
353 | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
354
355 Example:
356
357 ```javascript
358 import { v7 as uuidv7 } from 'uuid';
359
360 uuidv7(); // ⇨ '01941f29-7c00-73e4-a310-744d2167fc5b'
361 ```
362
363 ### ~~uuid.v8()~~
364
365 **_"Intentionally left blank"_**
366
367 <!-- prettier-ignore -->
368 > [!NOTE]
369 > Version 8 (experimental) UUIDs are "[for experimental or vendor-specific use cases](https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-8)". The RFC does not define a creation algorithm for them, which is why this package does not offer a `v8()` method. The `validate()` and `version()` methods do work with such UUIDs, however.
370
371 ### uuid.validate(str)
372
373 Test a string to see if it is a valid UUID
374
375 | | |
376 | --------- | --------------------------------------------------- |
377 | `str` | `String` to validate |
378 | _returns_ | `true` if string is a valid UUID, `false` otherwise |
379
380 Example:
381
382 ```javascript
383 import { validate as uuidValidate } from 'uuid';
384
385 uuidValidate('not a UUID'); // ⇨ false
386 uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true
387 ```
388
389 Using `validate` and `version` together it is possible to do per-version validation, e.g. validate for only v4 UUIds.
390
391 ```javascript
392 import { version as uuidVersion } from 'uuid';
393 import { validate as uuidValidate } from 'uuid';
394
395 function uuidValidateV4(uuid) {
396 return uuidValidate(uuid) && uuidVersion(uuid) === 4;
397 }
398
399 const v1Uuid = 'd9428888-122b-11e1-b85c-61cd3cbb3210';
400 const v4Uuid = '109156be-c4fb-41ea-b1b4-efe1671c5836';
401
402 uuidValidateV4(v4Uuid); // ⇨ true
403 uuidValidateV4(v1Uuid); // ⇨ false
404 ```
405
406 ### uuid.version(str)
407
408 Detect RFC version of a UUID
409
410 | | |
411 | --------- | ---------------------------------------- |
412 | `str` | A valid UUID `String` |
413 | _returns_ | `Number` The RFC version of the UUID |
414 | _throws_ | `TypeError` if `str` is not a valid UUID |
415
416 Example:
417
418 ```javascript
419 import { version as uuidVersion } from 'uuid';
420
421 uuidVersion('45637ec4-c85f-11ea-87d0-0242ac130003'); // ⇨ 1
422 uuidVersion('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ 4
423 ```
424
425 <!-- prettier-ignore -->
426 > [!NOTE]
427 > This method returns `0` for the `NIL` UUID, and `15` for the `MAX` UUID.
428
429 ## Command Line
430
431 UUIDs can be generated from the command line using `uuid`.
432
433 ```shell
434 $ npx uuid
435 ddeb27fb-d9a0-4624-be4d-4615062daed4
436 ```
437
438 The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
439
440 ```shell
441 $ npx uuid --help
442
443 Usage:
444 uuid
445 uuid v1
446 uuid v3 <name> <namespace uuid>
447 uuid v4
448 uuid v5 <name> <namespace uuid>
449 uuid v7
450 uuid --help
451
452 Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
453 defined by RFC9562
454 ```
455
456 ## `options` Handling for Timestamp UUIDs
457
458 Prior to `uuid@11`, it was possible for `options` state to interfere with the internal state used to ensure uniqueness of timestamp-based UUIDs (the `v1()`, `v6()`, and `v7()` methods). Starting with `uuid@11`, this issue has been addressed by using the presence of the `options` argument as a flag to select between two possible behaviors:
459
460 - Without `options`: Internal state is utilized to improve UUID uniqueness.
461 - With `options`: Internal state is **NOT** used and, instead, appropriate defaults are applied as needed.
462
463 ## Support
464
465 **Browsers**: `uuid` [builds are tested](/uuidjs/uuid/blob/main/wdio.conf.js) against the latest version of desktop Chrome, Safari, Firefox, and Edge. Mobile versions of these same browsers are expected to work but aren't currently tested.
466
467 **Node**: `uuid` [builds are tested](https://github.com/uuidjs/uuid/blob/main/.github/workflows/ci.yml#L31) against node ([LTS releases](https://github.com/nodejs/Release)), plus one prior. E.g. At the time of this writing `node@20` is the "maintenance" release and `node@24` is the "current" release, so `uuid` supports `node@20`-`node@24`.
468
469 **Typescript**: TS versions released within the past two years are supported. [source](https://github.com/microsoft/TypeScript/issues/49088#issuecomment-2468723715)
470
471 ## Known issues
472
473 <!-- This header is referenced as an anchor in src/rng.ts -->
474
475 ### "getRandomValues() not supported"
476
477 This error occurs in environments where the standard [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) API is not supported. This issue can be resolved by adding an appropriate polyfill:
478
479 #### React Native / Expo
480
481 1. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
482 1. Import it _before_ `uuid`. Since `uuid` might also appear as a transitive dependency of some other imports it's safest to just import `react-native-get-random-values` as the very first thing in your entry point:
483
484 ```javascript
485 import 'react-native-get-random-values';
486 import { v4 as uuidv4 } from 'uuid';
487 ```
488
489 ---
490
491 Generated from [README_js.md](README_js.md) by [`runmd`](https://github.com/broofa/runmd)