Skip to content

Commit

Permalink
feat: isolate state instead of using a wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ctavan committed Mar 30, 2020
1 parent 4e0bc15 commit d6aa52a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 29 deletions.
1 change: 0 additions & 1 deletion .local/wrapper.mjs

This file was deleted.

53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
"main": "./dist/index.js",
"exports": {
"require": "./dist/index.js",
"import": "./wrapper.mjs"
"import": "./dist/esm-node/index.mjs"
},
"module": "dist/esm-node/index.js",
"module": "./dist/esm-node/index.mjs",
"browser": {
"./dist/md5.js": "./dist/md5-browser.js",
"./dist/rng.js": "./dist/rng-browser.js",
"./dist/sha1.js": "./dist/sha1-browser.js",
"./dist/esm-node/index.js": "./dist/esm-browser/index.js"
"./dist/esm-node/index.mjs": "./dist/esm-browser/index.js"
},
"files": [
"CHANGELOG.md",
Expand All @@ -38,15 +38,15 @@
"v1.js",
"v3.js",
"v4.js",
"v5.js",
"wrapper.mjs"
"v5.js"
],
"devDependencies": {
"@babel/cli": "7.8.4",
"@babel/core": "7.8.7",
"@babel/preset-env": "7.8.7",
"@commitlint/cli": "8.3.5",
"@commitlint/config-conventional": "8.3.4",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-node-resolve": "7.1.1",
"@wdio/browserstack-service": "5.18.7",
"@wdio/cli": "5.18.7",
Expand Down
9 changes: 8 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';

function chunk(input, name) {
Expand All @@ -10,7 +11,13 @@ function chunk(input, name) {
name,
compact: true,
},
plugins: [nodeResolve({ browser: true }), terser()],
plugins: [
nodeResolve({ browser: true }),
commonjs({
extensions: ['.cjs'],
}),
terser(),
],
};
}

Expand Down
10 changes: 10 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ done
echo "Removing browser-specific files from esm-node"
rm -f "$DIR"/esm-node/*-browser.js

for FILE in "$DIR"/esm-node/*.js
do
echo "Renaming esm-node file to .mjs: $FILE"
mv "$FILE" "${FILE%.js}.mjs"
done
# Fix imports
sed -i '' -e "s/\.js'/.mjs'/" "$DIR"/esm-node/*.mjs
sed -i '' -e "s/\.\/v1state.cjs'/..\/v1state.cjs'/" "$DIR"/esm-node/v1.mjs
rm -f "$DIR"/esm-node/v1state.cjs

# UMD Build
mkdir "$DIR/umd"
rollup -c
28 changes: 11 additions & 17 deletions src/v1.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import rng from './rng.js';
import bytesToUuid from './bytesToUuid.js';
import state from './v1state.cjs';

// **`v1()` - Generate time-based UUID**
//
// Inspired by https://github.com/LiosK/UUID.js
// and http://docs.python.org/library/uuid.html

var _nodeId;
var _clockseq;

// Previous uuid creation time
var _lastMSecs = 0;
var _lastNSecs = 0;

// See https://github.com/uuidjs/uuid for API details
function v1(options, buf, offset) {
var i = (buf && offset) || 0;
var b = buf || [];

options = options || {};
var node = options.node || _nodeId;
var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
var node = options.node || state._nodeId;
var clockseq = options.clockseq !== undefined ? options.clockseq : state._clockseq;

// node and clockseq need to be initialized to random values if they're not
// specified. We do this lazily to minimize issues related to insufficient
Expand All @@ -29,7 +23,7 @@ function v1(options, buf, offset) {
var seedBytes = options.random || (options.rng || rng)();
if (node == null) {
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
node = _nodeId = [
node = state._nodeId = [
seedBytes[0] | 0x01,
seedBytes[1],
seedBytes[2],
Expand All @@ -40,7 +34,7 @@ function v1(options, buf, offset) {
}
if (clockseq == null) {
// Per 4.2.2, randomize (14 bit) clockseq
clockseq = _clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff;
clockseq = state._clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff;
}
}

Expand All @@ -52,10 +46,10 @@ function v1(options, buf, offset) {

// Per 4.2.1.2, use count of uuid's generated during the current clock
// cycle to simulate higher resolution clock
var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
var nsecs = options.nsecs !== undefined ? options.nsecs : state._lastNSecs + 1;

// Time since last uuid creation (in msecs)
var dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000;
var dt = msecs - state._lastMSecs + (nsecs - state._lastNSecs) / 10000;

// Per 4.2.1.2, Bump clockseq on clock regression
if (dt < 0 && options.clockseq === undefined) {
Expand All @@ -64,7 +58,7 @@ function v1(options, buf, offset) {

// Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
// time interval
if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
if ((dt < 0 || msecs > state._lastMSecs) && options.nsecs === undefined) {
nsecs = 0;
}

Expand All @@ -73,9 +67,9 @@ function v1(options, buf, offset) {
throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
}

_lastMSecs = msecs;
_lastNSecs = nsecs;
_clockseq = clockseq;
state._lastMSecs = msecs;
state._lastNSecs = nsecs;
state._clockseq = clockseq;

// Per 4.1.4 - Convert from unix epoch to Gregorian epoch
msecs += 12219292800000;
Expand Down
7 changes: 7 additions & 0 deletions src/v1state.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = exports.default = {
_nodeId: undefined,
_clockseq: undefined,
// Previous uuid creation time
_lastMSecs: 0,
_lastNSecs: 0,
};
5 changes: 0 additions & 5 deletions wrapper.mjs

This file was deleted.

0 comments on commit d6aa52a

Please sign in to comment.