Skip to content

Commit 10fe59d

Browse files
committed
Change bundling technique
1 parent a2d11a5 commit 10fe59d

4 files changed

Lines changed: 175 additions & 19 deletions

File tree

lib/index.esm.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
function createError(message) {
2+
var err = new Error(message);
3+
err.source = "ulid";
4+
return err;
5+
}
6+
// These values should NEVER change. If
7+
// they do, we're no longer making ulids!
8+
var ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
9+
var ENCODING_LEN = ENCODING.length;
10+
var TIME_MAX = Math.pow(2, 48) - 1;
11+
var TIME_LEN = 10;
12+
var RANDOM_LEN = 16;
13+
function replaceCharAt(str, index, char) {
14+
if (index > str.length - 1) {
15+
return str;
16+
}
17+
return str.substr(0, index) + char + str.substr(index + 1);
18+
}
19+
function incrementBase32(str) {
20+
var done = undefined;
21+
var index = str.length;
22+
var char = void 0;
23+
var charIndex = void 0;
24+
var maxCharIndex = ENCODING_LEN - 1;
25+
while (!done && index-- >= 0) {
26+
char = str[index];
27+
charIndex = ENCODING.indexOf(char);
28+
if (charIndex === -1) {
29+
throw createError("incorrectly encoded string");
30+
}
31+
if (charIndex === maxCharIndex) {
32+
str = replaceCharAt(str, index, ENCODING[0]);
33+
continue;
34+
}
35+
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
36+
}
37+
if (typeof done === "string") {
38+
return done;
39+
}
40+
throw createError("cannot increment this string");
41+
}
42+
function randomChar(prng) {
43+
var rand = Math.floor(prng() * ENCODING_LEN);
44+
if (rand === ENCODING_LEN) {
45+
rand = ENCODING_LEN - 1;
46+
}
47+
return ENCODING.charAt(rand);
48+
}
49+
function encodeTime(now, len) {
50+
if (isNaN(now)) {
51+
throw new Error(now + " must be a number");
52+
}
53+
if (now > TIME_MAX) {
54+
throw createError("cannot encode time greater than " + TIME_MAX);
55+
}
56+
if (now < 0) {
57+
throw createError("time must be positive");
58+
}
59+
if (Number.isInteger(now) === false) {
60+
throw createError("time must be an integer");
61+
}
62+
var mod = void 0;
63+
var str = "";
64+
for (; len > 0; len--) {
65+
mod = now % ENCODING_LEN;
66+
str = ENCODING.charAt(mod) + str;
67+
now = (now - mod) / ENCODING_LEN;
68+
}
69+
return str;
70+
}
71+
function encodeRandom(len, prng) {
72+
var str = "";
73+
for (; len > 0; len--) {
74+
str = randomChar(prng) + str;
75+
}
76+
return str;
77+
}
78+
function decodeTime(id) {
79+
if (id.length !== TIME_LEN + RANDOM_LEN) {
80+
throw createError("malformed ulid");
81+
}
82+
var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) {
83+
var encodingIndex = ENCODING.indexOf(char);
84+
if (encodingIndex === -1) {
85+
throw createError("invalid character found: " + char);
86+
}
87+
return carry += encodingIndex * Math.pow(ENCODING_LEN, index);
88+
}, 0);
89+
if (time > TIME_MAX) {
90+
throw createError("malformed ulid, timestamp too large");
91+
}
92+
return time;
93+
}
94+
function detectPrng() {
95+
var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
96+
var root = arguments[1];
97+
98+
if (!root) {
99+
root = typeof window !== "undefined" ? window : null;
100+
}
101+
var browserCrypto = root && (root.crypto || root.msCrypto);
102+
if (browserCrypto) {
103+
try {
104+
return function () {
105+
return browserCrypto.getRandomValues(new Uint8Array(1))[0] / 0xff;
106+
};
107+
} catch (e) {}
108+
} else {
109+
try {
110+
var nodeCrypto = require("crypto");
111+
return function () {
112+
return nodeCrypto.randomBytes(1).readUInt8() / 0xff;
113+
};
114+
} catch (e) {}
115+
}
116+
if (allowInsecure) {
117+
try {
118+
console.error("secure crypto unusable, falling back to insecure Math.random()!");
119+
} catch (e) {}
120+
return function () {
121+
return Math.random();
122+
};
123+
}
124+
throw createError("secure crypto unusable, insecure Math.random not allowedW");
125+
}
126+
function factory(currPrng) {
127+
if (!currPrng) {
128+
currPrng = detectPrng();
129+
}
130+
return function ulid(seedTime) {
131+
if (isNaN(seedTime)) {
132+
seedTime = Date.now();
133+
}
134+
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
135+
};
136+
}
137+
function monotonicFactory(currPrng) {
138+
if (!currPrng) {
139+
currPrng = detectPrng();
140+
}
141+
var lastTime = 0;
142+
var lastRandom = void 0;
143+
return function ulid(seedTime) {
144+
if (isNaN(seedTime)) {
145+
seedTime = Date.now();
146+
}
147+
if (seedTime <= lastTime) {
148+
var incrementedRandom = lastRandom = incrementBase32(lastRandom);
149+
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
150+
}
151+
lastTime = seedTime;
152+
var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng);
153+
return encodeTime(seedTime, TIME_LEN) + newRandom;
154+
};
155+
}
156+
var ulid = factory();
157+
158+
export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid };

lib/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ export function detectPrng(allowInsecure = false, root) {
102102
const browserCrypto = root && (root.crypto || root.msCrypto);
103103
if (browserCrypto) {
104104
try {
105-
return () => {
106-
const buffer = new Uint8Array(1);
107-
browserCrypto.getRandomValues(buffer);
108-
return buffer[0] / 0xff;
109-
};
105+
return () => browserCrypto.getRandomValues(new Uint8Array(1))[0] / 0xff;
110106
}
111107
catch (e) { }
112108
}
@@ -124,7 +120,7 @@ export function detectPrng(allowInsecure = false, root) {
124120
catch (e) { }
125121
return () => Math.random();
126122
}
127-
throw createError("secure crypto unusable, insecure Math.random not allowed");
123+
throw createError("secure crypto unusable, insecure Math.random not allowedW");
128124
}
129125
export function factory(currPrng) {
130126
if (!currPrng) {

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"version": "2.2.3",
44
"description": "A universally-unique, lexicographically-sortable, identifier generator",
55
"main": "./lib/index.umd.js",
6-
"module": "./lib/index.js",
6+
"module": "./lib/index.esm.js",
77
"types": "./lib/index.d.ts",
8+
"esnext": "./lib/index.js",
89
"repository": {
910
"type": "git",
1011
"url": "git+https://github.com/ulid/javascript.git"
@@ -32,8 +33,8 @@
3233
},
3334
"scripts": {
3435
"ts": "./node_modules/.bin/tsc -p .",
35-
"umd": "./node_modules/.bin/rollup -c",
36-
"build": "npm run ts && npm run umd",
36+
"rollup": "./node_modules/.bin/rollup -c",
37+
"build": "npm run ts && npm run rollup",
3738
"test": "./node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
3839
"perf": "./node_modules/.bin/matcha perf.js"
3940
},

rollup.config.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ const defaultPlugins = [
1212
typescript({ typescript: compiler })
1313
]
1414

15-
// const es6Config = Object.assign({}, defaultConfig, {
16-
// output: {
17-
// format: 'es',
18-
// file: './lib/ulid.es6.js'
19-
// },
20-
// plugins: [
21-
// ...defaultPlugins
22-
// ]
23-
// })
15+
const esModuleConfig = Object.assign({}, defaultConfig, {
16+
output: {
17+
format: 'es',
18+
file: './lib/index.esm.js'
19+
},
20+
plugins: [
21+
...defaultPlugins,
22+
babel()
23+
]
24+
})
2425

2526
const umdConfig = Object.assign({}, defaultConfig, {
2627
output: {
@@ -34,6 +35,6 @@ const umdConfig = Object.assign({}, defaultConfig, {
3435
})
3536

3637
export default [
37-
// es6Config,
38+
esModuleConfig,
3839
umdConfig
3940
]

0 commit comments

Comments
 (0)