diff --git a/.local/wrapper.mjs b/.local/wrapper.mjs deleted file mode 120000 index 4d9f862a..00000000 --- a/.local/wrapper.mjs +++ /dev/null @@ -1 +0,0 @@ -../wrapper.mjs \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 578ae625..a11353a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3086,6 +3086,27 @@ "rimraf": "^2.5.2" } }, + "@rollup/plugin-commonjs": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-11.0.2.tgz", + "integrity": "sha512-MPYGZr0qdbV5zZj8/2AuomVpnRVXRU5XKXb3HVniwRoRCreGlf5kOE081isNWeiLIi6IYkwTX9zE0/c7V8g81g==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.0.0", + "estree-walker": "^1.0.1", + "is-reference": "^1.1.2", + "magic-string": "^0.25.2", + "resolve": "^1.11.0" + }, + "dependencies": { + "estree-walker": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-1.0.1.tgz", + "integrity": "sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==", + "dev": true + } + } + }, "@rollup/plugin-node-resolve": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.1.tgz", @@ -8905,6 +8926,23 @@ "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=", "dev": true }, + "is-reference": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.1.4.tgz", + "integrity": "sha512-uJA/CDPO3Tao3GTrxYn6AwkM4nUPJiGGYu5+cB8qbC7WGFlrKZbiRo7SFKxUAEpFUfiHofWCXBUNhvYJMh+6zw==", + "dev": true, + "requires": { + "@types/estree": "0.0.39" + }, + "dependencies": { + "@types/estree": { + "version": "0.0.39", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", + "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", + "dev": true + } + } + }, "is-regex": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", @@ -11480,6 +11518,15 @@ "yallist": "^2.1.2" } }, + "magic-string": { + "version": "0.25.7", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz", + "integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==", + "dev": true, + "requires": { + "sourcemap-codec": "^1.4.4" + } + }, "make-dir": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz", @@ -13414,6 +13461,12 @@ "integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=", "dev": true }, + "sourcemap-codec": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz", + "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", + "dev": true + }, "spdx-correct": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.0.0.tgz", diff --git a/package.json b/package.json index 6984cd5c..2ce3433a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -38,8 +38,7 @@ "v1.js", "v3.js", "v4.js", - "v5.js", - "wrapper.mjs" + "v5.js" ], "devDependencies": { "@babel/cli": "7.8.4", @@ -47,6 +46,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", diff --git a/rollup.config.js b/rollup.config.js index 34544dfd..59f8457b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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) { @@ -10,7 +11,13 @@ function chunk(input, name) { name, compact: true, }, - plugins: [nodeResolve({ browser: true }), terser()], + plugins: [ + nodeResolve({ browser: true }), + commonjs({ + extensions: ['.cjs'], + }), + terser(), + ], }; } diff --git a/scripts/build.sh b/scripts/build.sh index 0efdbfea..6073fbbe 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -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 diff --git a/src/v1.js b/src/v1.js index 594616e7..92cf4b2f 100644 --- a/src/v1.js +++ b/src/v1.js @@ -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 @@ -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], @@ -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; } } @@ -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) { @@ -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; } @@ -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; diff --git a/src/v1state.cjs b/src/v1state.cjs new file mode 100644 index 00000000..7493cc19 --- /dev/null +++ b/src/v1state.cjs @@ -0,0 +1,7 @@ +module.exports = exports.default = { + _nodeId: undefined, + _clockseq: undefined, + // Previous uuid creation time + _lastMSecs: 0, + _lastNSecs: 0, +}; diff --git a/wrapper.mjs b/wrapper.mjs deleted file mode 100644 index 87e7f2e7..00000000 --- a/wrapper.mjs +++ /dev/null @@ -1,5 +0,0 @@ -import uuid from './dist/index.js'; -export const v1 = uuid.v1; -export const v3 = uuid.v3; -export const v4 = uuid.v4; -export const v5 = uuid.v5;