-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
143 lines (130 loc) · 3.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(function () {
"use strict";
var crypto = require('crypto');
var assert = require('assert');
var cryptoRandomBytes = crypto.pseudoRandomBytes || crypto.randomBytes;
var numeric = '0123456789';
var alphaLower = 'abcdefghijklmnopqrstuvwxyz';
var alphaUpper = alphaLower.toUpperCase();
// NOTE: This is explicitly in sortable order:
var alphaNumeric = numeric + alphaUpper + alphaLower;
var defaults = {
"chars": 'default',
"source": 'default'
};
function validateTokenChars(tokenChars) {
assert(tokenChars);
assert(typeof(tokenChars) == 'string');
assert(tokenChars.length > 0);
assert(tokenChars.length < 256);
}
function bufferAlloc(size) {
if (Buffer.alloc) {
return Buffer.alloc(size);
}
return new Buffer(size);
}
function buildGenerator(options) {
assert(!options || typeof(options) == 'object');
options = options || {};
options.chars = options.chars || defaults.chars;
options.source = options.source || defaults.source;
// Allowed characters
switch( options.chars ) {
case 'default':
options.chars = alphaNumeric;
break;
case 'a-z':
case 'alpha':
options.chars = alphaLower;
break;
case 'A-Z':
case 'ALPHA':
options.chars = alphaUpper;
break;
case '0-9':
case 'numeric':
options.chars = numeric;
break;
case 'base32':
options.chars = alphaUpper + "234567";
break;
default:
// use the characters as is
}
validateTokenChars(options.chars);
// Source of randomness:
switch( options.source ) {
case 'default':
options.source = cryptoRandomBytes;
break;
case 'crypto':
options.source = crypto.randomBytes;
break;
case 'math':
options.source = function(size) {
var buf = bufferAlloc(size);
for(var i=0;i<size;i++) {
buf.writeUInt8(Math.floor(256 * Math.random()), i);
}
return buf;
};
break;
default:
assert(typeof(options.source) == 'function');
}
return {
"generate": function(size, chars) {
if( chars ) {
validateTokenChars(chars);
} else {
chars = options.chars;
}
var max = Math.floor(256 / chars.length) * chars.length;
var ret = "";
while( ret.length < size ) {
var buf = options.source(size - ret.length);
for(var i=0;i<buf.length;i++) {
var x = buf.readUInt8(i);
if( x < max ) {
ret += chars[x % chars.length];
}
}
}
return ret;
}
};
}
function base62(n) {
assert(n >= 0);
n = Math.floor(n);
var ret = [];
do {
var index = n % 62;
ret.push(alphaNumeric[index]);
n = Math.floor(n / 62);
} while( n > 0);
return ret.reverse().join("");
}
// Default epoch of "2000-01-01T00:00:00+00:00"
var defaultEpoch = 946684800000;
var defaultPrefixLength = 8;
function suidPrefix(epoch, prefixLength) {
var ret = base62(Date.now() - epoch);
while( ret.length < prefixLength ) {
ret = "0" + ret;
}
return ret;
}
var defaultGenerator = buildGenerator();
module.exports = {
generator: buildGenerator,
generate: defaultGenerator.generate,
uid: defaultGenerator.generate,
suid: function(length, epoch, prefixLength) {
epoch = epoch || defaultEpoch;
prefixLength = prefixLength || defaultPrefixLength;
return suidPrefix(epoch, prefixLength) + defaultGenerator.generate(length);
}
};
})();