Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 12 KiB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <!--
  2. -- This file is auto-generated from README_js.md. Changes should be made there.
  3. -->
  4. # uuid [![Build Status](https://github.com/uuidjs/uuid/workflows/CI/badge.svg)](https://github.com/uuidjs/uuid/actions)
  5. For the creation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDs
  6. - **Complete** - Support for RFC4122 version 1, 3, 4, and 5 UUIDs
  7. - **Cross-platform** - Support for ...
  8. - CommonJS, [ECMAScript Modules](#ecmascript-modules) and UMD builds
  9. - Node 8, 10, 12
  10. - Chrome, Safari, Firefox, Edge, IE 11 browsers
  11. - Webpack and rollup.js module bundlers
  12. - [React Native](#react-native)
  13. - **Secure** - Cryptographically-strong random values
  14. - **Small** - Zero-dependency, small footprint, plays nice with "tree shaking" packagers
  15. - **CLI** - Includes the [`uuid` command line](#command-line) utility
  16. **Upgrading from uuid\@3?** Your code is probably okay, but check out [Upgrading
  17. From uuid\@3](#upgrading-from-uuid3) for details.
  18. ## Quickstart - Node.js/CommonJS
  19. ```shell
  20. npm install uuid
  21. ```
  22. Once installed, decide which type of UUID you need. RFC4122 provides for four
  23. versions, all of which are supported here. In order of popularity, they are:
  24. - Version 4 (random) - Created from cryptographically-strong random values
  25. - Version 1 (timestamp) - Created from the system clock (plus random values)
  26. - Version 5 (namespace, SHA-1) - Created from user-supplied name and namespace strings
  27. - Version 3 (namespace, MD5) - Like version 5, above, but with a poorer hash algorithm
  28. **Unsure which one to use?** Use version 4 (random) unless you have a specific need for one of the other versions. See also [this FAQ](https://github.com/tc39/proposal-uuid#faq).
  29. ### Create Version 4 (Random) UUIDs
  30. ```javascript
  31. import { v4 as uuidv4 } from 'uuid';
  32. uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
  33. ```
  34. ### Create Version 1 (Timestamp) UUIDs
  35. ```javascript
  36. import { v1 as uuidv1 } from 'uuid';
  37. uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8b2d-1b9d6bcdbbfd'
  38. ```
  39. ### Create Version 3 or Version 5 (Namespace) UUIDs
  40. &#x26a0;&#xfe0f; Version 3 and Version 5 UUIDs are basically the same, differing
  41. only in the underlying hash algorithm. Note that per the RFC, "_If backward
  42. compatibility is not an issue, SHA-1 [Version 5] is preferred_."
  43. &#x26a0;&#xfe0f; If using a custom namespace **be sure to generate your own
  44. namespace UUID**. You can grab one [here](https://www.uuidgenerator.net/).
  45. ```javascript
  46. import { v5 as uuidv5 } from 'uuid'; // For version 5
  47. import { v3 as uuidv3 } from 'uuid'; // For version 3
  48. // Using predefined DNS namespace (for domain names)
  49. uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
  50. uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
  51. // Using predefined URL namespace (for URLs)
  52. uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
  53. uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
  54. // Using a custom namespace (See note, above, about generating your own
  55. // namespace UUID)
  56. const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
  57. uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
  58. uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
  59. ```
  60. ## API
  61. ### Version 4 (Random)
  62. ```javascript
  63. import { v4 as uuidv4 } from 'uuid';
  64. // Incantations
  65. uuidv4();
  66. uuidv4(options);
  67. uuidv4(options, buffer, offset);
  68. ```
  69. Generate and return a RFC4122 version 4 UUID.
  70. - `options` - (Object) Optional uuid state to apply. Properties may include:
  71. - `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values. Takes precedence over `options.rng`.
  72. - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
  73. - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
  74. - `offset` - (Number) Starting index in `buffer` at which to begin writing.
  75. Returns `buffer`, if specified, otherwise the string form of the UUID
  76. Example: Generate string UUID with predefined `random` values
  77. ```javascript
  78. const v4options = {
  79. random: [
  80. 0x10,
  81. 0x91,
  82. 0x56,
  83. 0xbe,
  84. 0xc4,
  85. 0xfb,
  86. 0xc1,
  87. 0xea,
  88. 0x71,
  89. 0xb4,
  90. 0xef,
  91. 0xe1,
  92. 0x67,
  93. 0x1c,
  94. 0x58,
  95. 0x36,
  96. ],
  97. };
  98. uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
  99. ```
  100. Example: Generate two IDs in a single buffer
  101. ```javascript
  102. const buffer = new Array();
  103. uuidv4(null, buffer, 0); // ⇨
  104. // [
  105. // 155, 29, 235, 77, 59,
  106. // 125, 75, 173, 155, 221,
  107. // 43, 13, 123, 61, 203,
  108. // 109
  109. // ]
  110. uuidv4(null, buffer, 16); // ⇨
  111. // [
  112. // 155, 29, 235, 77, 59, 125, 75, 173,
  113. // 155, 221, 43, 13, 123, 61, 203, 109,
  114. // 27, 157, 107, 205, 187, 253, 75, 45,
  115. // 155, 93, 171, 141, 251, 189, 75, 237
  116. // ]
  117. ```
  118. ### Version 1 (Timestamp)
  119. ```javascript
  120. import { v1 as uuidv1 } from 'uuid';
  121. // Incantations
  122. uuidv1();
  123. uuidv1(options);
  124. uuidv1(options, buffer, offset);
  125. ```
  126. Generate and return a RFC4122 version 1 (timestamp) UUID.
  127. - `options` - (Object) Optional uuid state to apply. Properties may include:
  128. - `node` - (Array) Node id as Array of 6 bytes (per 4.1.6). Default: Randomly generated ID. See note 1.
  129. - `clockseq` - (Number between 0 - 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
  130. - `msecs` - (Number) Time in milliseconds since unix Epoch. Default: The current time is used.
  131. - `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.
  132. - `random` - (Number[16]) Array of 16 numbers (0-255) to use for initialization of `node` and `clockseq` as described above. Takes precedence over `options.rng`.
  133. - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
  134. - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
  135. - `offset` - (Number) Starting index in `buffer` at which to begin writing.
  136. Returns `buffer`, if specified, otherwise the string form of the UUID
  137. Note: The default [node id](https://tools.ietf.org/html/rfc4122#section-4.1.6) (the last 12 digits in the UUID) is generated once, randomly, on process startup, and then remains unchanged for the duration of the process.
  138. Example: Generate string UUID with fully-specified options
  139. ```javascript
  140. const v1options = {
  141. node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
  142. clockseq: 0x1234,
  143. msecs: new Date('2011-11-01').getTime(),
  144. nsecs: 5678,
  145. };
  146. uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
  147. ```
  148. Example: In-place generation of two binary IDs
  149. ```javascript
  150. // Generate two ids in an array
  151. const arr = new Array();
  152. uuidv1(null, arr, 0); // ⇨
  153. // [
  154. // 44, 94, 164, 192, 64,
  155. // 103, 17, 233, 146, 52,
  156. // 27, 157, 107, 205, 187,
  157. // 253
  158. // ]
  159. uuidv1(null, arr, 16); // ⇨
  160. // [
  161. // 44, 94, 164, 192, 64, 103, 17, 233,
  162. // 146, 52, 27, 157, 107, 205, 187, 253,
  163. // 44, 94, 164, 193, 64, 103, 17, 233,
  164. // 146, 52, 27, 157, 107, 205, 187, 253
  165. // ]
  166. ```
  167. ### Version 5 (Namespace)
  168. ```javascript
  169. import { v5 as uuidv5 } from 'uuid';
  170. // Incantations
  171. uuidv5(name, namespace);
  172. uuidv5(name, namespace, buffer);
  173. uuidv5(name, namespace, buffer, offset);
  174. ```
  175. Generate and return a RFC4122 version 5 UUID.
  176. - `name` - (String | Array[]) "name" to create UUID with
  177. - `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
  178. - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
  179. - `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
  180. Returns `buffer`, if specified, otherwise the string form of the UUID
  181. Example:
  182. ```javascript
  183. uuidv5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
  184. ```
  185. ### Version 3 (Namespace)
  186. &#x26a0;&#xfe0f; Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
  187. ```javascript
  188. import { v3 as uuidv3 } from 'uuid';
  189. // Incantations
  190. uuidv3(name, namespace);
  191. uuidv3(name, namespace, buffer);
  192. uuidv3(name, namespace, buffer, offset);
  193. ```
  194. Generate and return a RFC4122 version 3 UUID.
  195. - `name` - (String | Array[]) "name" to create UUID with
  196. - `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
  197. - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
  198. - `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
  199. Returns `buffer`, if specified, otherwise the string form of the UUID
  200. Example:
  201. ```javascript
  202. uuidv3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
  203. ```
  204. ## Command Line
  205. UUIDs can be generated from the command line using `uuid`.
  206. ```shell
  207. $ uuid
  208. ddeb27fb-d9a0-4624-be4d-4615062daed4
  209. ```
  210. The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
  211. ```
  212. $ uuid --help
  213. Usage:
  214. uuid
  215. uuid v1
  216. uuid v3 <name> <namespace uuid>
  217. uuid v4
  218. uuid v5 <name> <namespace uuid>
  219. uuid --help
  220. Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
  221. defined by RFC4122
  222. ```
  223. ## ECMAScript Modules
  224. For usage in the browser `uuid` provides support for [ECMAScript
  225. Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) that enable
  226. tree-shaking for bundlers, like [rollup.js](https://rollupjs.org/guide/en/#tree-shaking)
  227. ([example](./examples/browser-rollup/)) and [webpack](https://webpack.js.org/guides/tree-shaking/)
  228. ([example](./examples/browser-webpack/)).
  229. ```javascript
  230. import { v4 as uuidv4 } from 'uuid';
  231. uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
  232. ```
  233. There is experimental native ESM support for [the browser](./examples/browser-esmodules/) but it
  234. should not be considered ready for production use and may change or disappear in future releases.
  235. To run the examples you must first create a dist build of this library in the module root:
  236. ```
  237. npm run build
  238. ```
  239. ## UMD Builds
  240. If you want to load a minified UMD build directly in the browser you can use one of the popular npm
  241. CDNs:
  242. ```html
  243. <script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
  244. <script>
  245. alert(uuidv4());
  246. </script>
  247. ```
  248. or
  249. ```html
  250. <script src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"></script>
  251. <script>
  252. alert(uuidv4());
  253. </script>
  254. ```
  255. Available bundles:
  256. - https://unpkg.com/uuid@latest/dist/umd/
  257. - https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/
  258. ## "getRandomValues() not supported"
  259. This error occurs in environments where the standard
  260. [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)
  261. API is not supported. This issue can be resolved by adding an appropriate polyfill:
  262. ### React Native
  263. 1. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
  264. 1. Import it before `uuid`:
  265. ```javascript
  266. import 'react-native-get-random-values';
  267. import { v4 as uuidv4 } from 'uuid';
  268. ```
  269. ### Web Workers / Service Workers (Edge <= 18)
  270. [In Edge <= 18, Web Crypto is not supported in Web Workers or Service
  271. Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
  272. you find one, please).
  273. ## Upgrading From uuid\@3
  274. "_Wait... what happened to uuid\@4 - uuid\@6?!?_"
  275. In order to avoid confusion with RFC [version 4](#version-4-random) and [version
  276. 5](#version-5-namespace) UUIDs, and a possible [version
  277. 6](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been
  278. skipped. Hence, how we're now at uuid\@7.
  279. ### Deep Requires Now Deprecated
  280. uuid\@3 encouraged the use of deep requires to minimize the bundle size of
  281. browser builds:
  282. ```javascript
  283. const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
  284. uuidv4();
  285. ```
  286. As of uuid\@7 this library now provides ECMAScript modules builds, which allow
  287. packagers like Webpack and Rollup to do "tree-shaking" to remove dead code.
  288. Instead, use the `import` syntax:
  289. ```javascript
  290. import { v4 as uuidv4 } from 'uuid';
  291. uuidv4();
  292. ```
  293. ... or for CommonJS:
  294. ```javascript
  295. const { v4: uuidv4 } = require('uuid');
  296. uuidv4();
  297. ```
  298. ### Default Export Removed
  299. uuid\@3 was exporting the Version 4 UUID method as a default export:
  300. ```javascript
  301. const uuid = require('uuid'); // <== REMOVED!
  302. ```
  303. This usage pattern was already discouraged in uuid\@3 and has been removed in uuid\@7.
  304. ----
  305. Markdown generated from [README_js.md](README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)