Codebase list node-uniqid / 62db859
v3 + support uniqueness even while running on unlimited number of concurrent machines with mac addresses + introducing uniqid.process() + introducing uniqid.time() Ádám Halász 8 years ago
5 changed file(s) with 53 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
0 /node_modules
1111 ```js
1212 var uniqid = require('uniqid');
1313
14 console.log(uniqid()); // -> 19fx7pio25mard
15 console.log(uniqid(), uniqid()); // -> 19fx7pio25mare, 19fz7pio25mark
14 console.log(uniqid()); // -> 4n5pxq24kpiob12og9
15 console.log(uniqid(), uniqid()); // -> 4n5pxq24kriob12ogd, 4n5pxq24ksiob12ogl
1616 ```
1717
1818 ## Features
19 - With the current time the ID's are always unique.
20 - With the Process PID the ID's are unique even if called at the same time from multiple processes.
21 - With the OS Hostname's first 3 letters the ID's are unique even if called at the same time from multiple machines if the OS Hostname is different.
19 - Very fast
20 - Generates unique id's on multiple processes and machines even if called at the same time. Generates 100% Unique IDs every time.
21 - Shorter 8 and 12 byte versions with less uniqueness.
22
23
24 # How it works
25 - With the current time the ID's are always unique in a single process.
26 - With the Process ID the ID's are unique even if called at the same time from multiple processes.
27 - With the MAC Address the ID's are unique even if called at the same time from multiple machines and processes.
2228
2329 ## API:
2430 #### **uniqid(** prefix *optional string* **)**
31 Generate 18 byte unique id's based on the time, process id and mac address. Works on multi processes and machines.
2532
2633 ```js
27 uniqid() -> "19fx7pio25mard"
28 uniqid('hello-') -> "hello-19fx7pio25mard"
34 uniqid() -> "4n5pxq24kpiob12og9"
35 uniqid('hello-') -> "hello-4n5pxq24kpiob12og9"
36 ```
37
38 #### **uniqid.process(** prefix *optional string* **)**
39 Generate 12 byte unique id's based on the time and the process id. Works on multiple processes within a single machine but not on multiple machines.
40 ```js
41 uniqid.process() -> "24ieiob0te82"
42 ```
43
44 #### **uniqid.time(** prefix *optional string* **)**
45 Generate 8 byte unique id's based on the current time only. Recommended only on a single process on a single machine.
46
47 ```js
48 uniqid.time() -> "iob0ucoj"
2949 ```
3050
3151 ## **License**
1515 } else {
1616 // Workers can share any TCP connection
1717 // In this case it is an HTTP server
18 console.log(process.pid, uniqid(), uniqid())
18 console.log('PID: ' + process.pid, 'ID-1: '+ uniqid(), 'ID-2: '+ uniqid())
1919 }
00 /*
11 (The MIT License)
2
32 Copyright (c) 2014 Halász Ádám <mail@adamhalasz.com>
4
53 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:
6
74 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
95 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.
106 */
117
128 // Unique Hexatridecimal ID Generator
13 var hostname = require('os').hostname().substr(0, 3);
14 var MachineProcessId = process.pid.toString(36) + (hostname.charCodeAt(0) + hostname.charCodeAt(1) + hostname.charCodeAt(2)).toString(36) ;
9 var pid = process.pid.toString(36), address;
10 require('macaddress').one(function(error, macAddress){ if(error) throw error; address = macAddress; });
11 while(!address); // block to support synchronous initialization
12 address = parseInt(address.replace(/\:|\D+/gi, '')).toString(36);
13
1514 module.exports = function(prefix){
15 return (prefix || '') + address + pid + now().toString(36);
16 }
17
18 module.exports.process = function(prefix){
19 return (prefix || '') + pid + now().toString(36);
20 }
21
22 module.exports.time = function(prefix){
23 return (prefix || '') + now().toString(36);
24 }
25
26 function now(){
1627 var time = new Date().getTime();
17 while (time == new Date().getTime());
18 return (prefix || '') + MachineProcessId + new Date().getTime().toString(36);
28 var last = now.last || time;
29 return now.last = time > last ? time : last + 1;
1930 }
00 {
11 "name": "uniqid",
2 "version": "2.0.0",
2 "version": "3.0.0",
33 "description": "Unique ID Generator",
44 "homepage": "http://github.com/adamhalasz/diet-uniqid/",
55 "keywords": [
2222 "email": "mail@adamhalasz.com",
2323 "url": "http://adamhalasz.com/"
2424 },
25 "main": "index.js"
25 "main": "index.js",
26 "dependencies": {
27 "macaddress": "^0.2.8"
28 }
2629 }