From 0ab0941481d94d7e2c6b47211d72bd810faeec7e Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Mon, 3 Feb 2025 17:45:43 +0000 Subject: [PATCH 1/7] refactor: :recycle: add esmodule support --- index.mjs | 61 +++++++++++++++++++++++++++++++++ lib/esm/fingerprint.browser.mjs | 15 ++++++++ lib/esm/fingerprint.mjs | 35 +++++++++++++++++++ lib/esm/is-cuid.mjs | 8 +++++ lib/esm/pad.mjs | 4 +++ package.json | 20 ++++++++++- 6 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 index.mjs create mode 100644 lib/esm/fingerprint.browser.mjs create mode 100644 lib/esm/fingerprint.mjs create mode 100644 lib/esm/is-cuid.mjs create mode 100644 lib/esm/pad.mjs diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..4129f65 --- /dev/null +++ b/index.mjs @@ -0,0 +1,61 @@ +/** + * cuid.js + * Collision-resistant UID generator for browsers and node. + * Sequential for fast db lookups and recency sorting. + * Safe for element IDs and server-side lookups. + * + * Extracted from CLCTR + * + * Copyright (c) Eric Elliott 2012 + * MIT License + */ + +import fingerprint from '#fingerprint'; +import isCuid from './lib/esm/is-cuid'; +import pad from './lib/esm/pad'; + +var c = 0, + blockSize = 4, + base = 36, + discreteValues = Math.pow(base, blockSize); + +function randomBlock () { + return pad((Math.random() * + discreteValues << 0) + .toString(base), blockSize); +} + +function safeCounter () { + c = c < discreteValues ? c : 0; + c++; // this is not subliminal + return c - 1; +} + +function cuid () { + // Starting with a lowercase letter makes + // it HTML element ID friendly. + var letter = 'c', // hard-coded allows for sequential access + + // timestamp + // warning: this exposes the exact date and time + // that the uid was created. + timestamp = new Date().getTime().toString(base), + + // Prevent same-machine collisions. + counter = pad(safeCounter().toString(base), blockSize), + + // A few chars to generate distinct ids for different + // clients (so different computers are far less + // likely to generate the same id) + print = fingerprint(), + + // Grab some more chars from Math.random() + random = randomBlock() + randomBlock(); + + return letter + timestamp + counter + print + random; +} + +cuid.fingerprint = fingerprint; +cuid.isCuid = isCuid; + +export default cuid; diff --git a/lib/esm/fingerprint.browser.mjs b/lib/esm/fingerprint.browser.mjs new file mode 100644 index 0000000..d5f184e --- /dev/null +++ b/lib/esm/fingerprint.browser.mjs @@ -0,0 +1,15 @@ +import pad from './pad.mjs'; + +var env = typeof window === 'object' ? window : self; +var globalCount = 0; +for (var prop in env) { + if (Object.hasOwnProperty.call(env, prop)) globalCount++; +} +var mimeTypesLength = navigator.mimeTypes ? navigator.mimeTypes.length : 0; +var clientId = pad((mimeTypesLength + + navigator.userAgent.length).toString(36) + + globalCount.toString(36), 4); + +export default function fingerprint () { + return clientId; +} diff --git a/lib/esm/fingerprint.mjs b/lib/esm/fingerprint.mjs new file mode 100644 index 0000000..8ebd9b7 --- /dev/null +++ b/lib/esm/fingerprint.mjs @@ -0,0 +1,35 @@ +import pad from './pad.mjs'; +import os from 'os'; + +function getHostname () { + try { + return os.hostname(); + } catch (e) { + /** + * This is most likely Windows 7 which is known to cause os.hostname() to break + * @see https://github.com/nodejs/node/issues/41297 + * @see https://github.com/libuv/libuv/issues/3260 + * + * Fallback to take hostname from environment variables + * @see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/hostname#notes + */ + // eslint-disable-next-line no-underscore-dangle + return process.env._CLUSTER_NETWORK_NAME_ || process.env.COMPUTERNAME || 'hostname'; + } +} + +var padding = 2, + pid = pad(process.pid.toString(36), padding), + hostname = getHostname(), + length = hostname.length, + hostId = pad(hostname + .split('') + .reduce(function (prev, char) { + return +prev + char.charCodeAt(0); + }, +length + 36) + .toString(36), + padding); + +export default function fingerprint () { + return pid + hostId; +} diff --git a/lib/esm/is-cuid.mjs b/lib/esm/is-cuid.mjs new file mode 100644 index 0000000..c7218dd --- /dev/null +++ b/lib/esm/is-cuid.mjs @@ -0,0 +1,8 @@ +/** + * Check the provided value is a valid device id + * @param {unknown} value + * @returns + */ +export default function isCuid (value) { + return typeof value === 'string' && (/^c[a-z0-9]{20,32}$/).test(value); +} diff --git a/lib/esm/pad.mjs b/lib/esm/pad.mjs new file mode 100644 index 0000000..1765436 --- /dev/null +++ b/lib/esm/pad.mjs @@ -0,0 +1,4 @@ +export default function pad (num, size) { + var s = '000000000' + num; + return s.substr(s.length - size); +} diff --git a/package.json b/package.json index 009efe9..92e9129 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,23 @@ }, "main": "index.js", "types": "cuid.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "default": "./index.js" + }, + "./lib/fingerprint": { + "browser": "./lib/esm/fingerprint.browser.mjs", + "node": "./lib/esm/fingerprint.mjs", + "default": "./lib/fingerprint.js" + } + }, + "imports": { + "#fingerprint": { + "browser": "./lib/esm/fingerprint.browser.mjs", + "default": "./lib/esm/fingerprint.mjs" + } + }, "browser": { "./lib/fingerprint.js": "./lib/fingerprint.browser.js" }, @@ -28,7 +45,8 @@ "files": [ "lib", "cuid.d.ts", - "index.js" + "index.js", + "index.mjs" ], "license": "MIT", "devDependencies": { From 337263bea3587f41032019c867c7cd6b17017b82 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 4 Feb 2025 15:22:21 +0000 Subject: [PATCH 2/7] refactor: :recycle: DRY: use rollup to create commonjs modules from esm --- index.esm.mjs | 18 ++++++ index.js | 61 ------------------ index.mjs | 49 +------------- lib/cuid.mjs | 64 +++++++++++++++++++ lib/fingerprint.browser.js | 15 ----- lib/{esm => }/fingerprint.browser.mjs | 2 +- lib/fingerprint.js | 35 ---------- lib/{esm => }/fingerprint.mjs | 2 +- ...native.js => fingerprint.react-native.mjs} | 6 +- lib/is-cuid.js | 8 --- lib/{esm => }/is-cuid.mjs | 0 lib/pad.js | 4 -- lib/{esm => }/pad.mjs | 0 package.json | 32 ++++++---- rollup.config.mjs | 30 +++++++++ 15 files changed, 138 insertions(+), 188 deletions(-) create mode 100644 index.esm.mjs delete mode 100644 index.js create mode 100644 lib/cuid.mjs delete mode 100644 lib/fingerprint.browser.js rename lib/{esm => }/fingerprint.browser.mjs (93%) delete mode 100644 lib/fingerprint.js rename lib/{esm => }/fingerprint.mjs (97%) rename lib/{fingerprint.react-native.js => fingerprint.react-native.mjs} (58%) delete mode 100644 lib/is-cuid.js rename lib/{esm => }/is-cuid.mjs (100%) delete mode 100644 lib/pad.js rename lib/{esm => }/pad.mjs (100%) create mode 100644 rollup.config.mjs diff --git a/index.esm.mjs b/index.esm.mjs new file mode 100644 index 0000000..1fa70eb --- /dev/null +++ b/index.esm.mjs @@ -0,0 +1,18 @@ +/** + * cuid.js + * Collision-resistant UID generator for browsers and node. + * Sequential for fast db lookups and recency sorting. + * Safe for element IDs and server-side lookups. + * + * Extracted from CLCTR + * + * Copyright (c) Eric Elliott 2012 + * MIT License + */ + +import fingerprint from '#fingerprint'; +import createCuid from './lib/cuid'; + +const cuid = createCuid(fingerprint); + +export default cuid; diff --git a/index.js b/index.js deleted file mode 100644 index 609c515..0000000 --- a/index.js +++ /dev/null @@ -1,61 +0,0 @@ -/** - * cuid.js - * Collision-resistant UID generator for browsers and node. - * Sequential for fast db lookups and recency sorting. - * Safe for element IDs and server-side lookups. - * - * Extracted from CLCTR - * - * Copyright (c) Eric Elliott 2012 - * MIT License - */ - -var fingerprint = require('./lib/fingerprint.js'); -var isCuid = require('./lib/is-cuid.js'); -var pad = require('./lib/pad.js'); - -var c = 0, - blockSize = 4, - base = 36, - discreteValues = Math.pow(base, blockSize); - -function randomBlock () { - return pad((Math.random() * - discreteValues << 0) - .toString(base), blockSize); -} - -function safeCounter () { - c = c < discreteValues ? c : 0; - c++; // this is not subliminal - return c - 1; -} - -function cuid () { - // Starting with a lowercase letter makes - // it HTML element ID friendly. - var letter = 'c', // hard-coded allows for sequential access - - // timestamp - // warning: this exposes the exact date and time - // that the uid was created. - timestamp = new Date().getTime().toString(base), - - // Prevent same-machine collisions. - counter = pad(safeCounter().toString(base), blockSize), - - // A few chars to generate distinct ids for different - // clients (so different computers are far less - // likely to generate the same id) - print = fingerprint(), - - // Grab some more chars from Math.random() - random = randomBlock() + randomBlock(); - - return letter + timestamp + counter + print + random; -} - -cuid.fingerprint = fingerprint; -cuid.isCuid = isCuid; - -module.exports = cuid; diff --git a/index.mjs b/index.mjs index 4129f65..4e16a47 100644 --- a/index.mjs +++ b/index.mjs @@ -10,52 +10,9 @@ * MIT License */ -import fingerprint from '#fingerprint'; -import isCuid from './lib/esm/is-cuid'; -import pad from './lib/esm/pad'; +import fingerprint from './lib/fingerprint'; +import createCuid from './lib/cuid'; -var c = 0, - blockSize = 4, - base = 36, - discreteValues = Math.pow(base, blockSize); - -function randomBlock () { - return pad((Math.random() * - discreteValues << 0) - .toString(base), blockSize); -} - -function safeCounter () { - c = c < discreteValues ? c : 0; - c++; // this is not subliminal - return c - 1; -} - -function cuid () { - // Starting with a lowercase letter makes - // it HTML element ID friendly. - var letter = 'c', // hard-coded allows for sequential access - - // timestamp - // warning: this exposes the exact date and time - // that the uid was created. - timestamp = new Date().getTime().toString(base), - - // Prevent same-machine collisions. - counter = pad(safeCounter().toString(base), blockSize), - - // A few chars to generate distinct ids for different - // clients (so different computers are far less - // likely to generate the same id) - print = fingerprint(), - - // Grab some more chars from Math.random() - random = randomBlock() + randomBlock(); - - return letter + timestamp + counter + print + random; -} - -cuid.fingerprint = fingerprint; -cuid.isCuid = isCuid; +const cuid = createCuid(fingerprint); export default cuid; diff --git a/lib/cuid.mjs b/lib/cuid.mjs new file mode 100644 index 0000000..2c539bf --- /dev/null +++ b/lib/cuid.mjs @@ -0,0 +1,64 @@ +/** + * cuid.js + * Collision-resistant UID generator for browsers and node. + * Sequential for fast db lookups and recency sorting. + * Safe for element IDs and server-side lookups. + * + * Extracted from CLCTR + * + * Copyright (c) Eric Elliott 2012 + * MIT License + */ + +import isCuid from './is-cuid'; +import pad from './pad'; + +export default function createCuid (fingerprint) { + const blockSize = 4, + base = 36, + discreteValues = Math.pow(base, blockSize); + + let c = 0; + + function randomBlock () { + return pad((Math.random() * + discreteValues << 0) + .toString(base), blockSize); + } + + function safeCounter () { + c = c < discreteValues ? c : 0; + c++; // this is not subliminal + return c - 1; + } + + function cuid () { + // Starting with a lowercase letter makes + // it HTML element ID friendly. + var letter = 'c', // hard-coded allows for sequential access + + // timestamp + // warning: this exposes the exact date and time + // that the uid was created. + timestamp = new Date().getTime().toString(base), + + // Prevent same-machine collisions. + counter = pad(safeCounter().toString(base), blockSize), + + // A few chars to generate distinct ids for different + // clients (so different computers are far less + // likely to generate the same id) + print = fingerprint(), + + // Grab some more chars from Math.random() + random = randomBlock() + randomBlock(); + + return letter + timestamp + counter + print + random; + } + + cuid.fingerprint = fingerprint; + cuid.isCuid = isCuid; + + return cuid; +} + diff --git a/lib/fingerprint.browser.js b/lib/fingerprint.browser.js deleted file mode 100644 index 048874e..0000000 --- a/lib/fingerprint.browser.js +++ /dev/null @@ -1,15 +0,0 @@ -var pad = require('./pad.js'); - -var env = typeof window === 'object' ? window : self; -var globalCount = 0; -for (var prop in env) { - if (Object.hasOwnProperty.call(env, prop)) globalCount++; -} -var mimeTypesLength = navigator.mimeTypes ? navigator.mimeTypes.length : 0; -var clientId = pad((mimeTypesLength + - navigator.userAgent.length).toString(36) + - globalCount.toString(36), 4); - -module.exports = function fingerprint () { - return clientId; -}; diff --git a/lib/esm/fingerprint.browser.mjs b/lib/fingerprint.browser.mjs similarity index 93% rename from lib/esm/fingerprint.browser.mjs rename to lib/fingerprint.browser.mjs index d5f184e..6702307 100644 --- a/lib/esm/fingerprint.browser.mjs +++ b/lib/fingerprint.browser.mjs @@ -1,4 +1,4 @@ -import pad from './pad.mjs'; +import pad from './pad'; var env = typeof window === 'object' ? window : self; var globalCount = 0; diff --git a/lib/fingerprint.js b/lib/fingerprint.js deleted file mode 100644 index e467b56..0000000 --- a/lib/fingerprint.js +++ /dev/null @@ -1,35 +0,0 @@ -var pad = require('./pad.js'); -var os = require('os'); - -function getHostname () { - try { - return os.hostname(); - } catch (e) { - /** - * This is most likely Windows 7 which is known to cause os.hostname() to break - * @see https://github.com/nodejs/node/issues/41297 - * @see https://github.com/libuv/libuv/issues/3260 - * - * Fallback to take hostname from environment variables - * @see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/hostname#notes - */ - // eslint-disable-next-line no-underscore-dangle - return process.env._CLUSTER_NETWORK_NAME_ || process.env.COMPUTERNAME || 'hostname'; - } -} - -var padding = 2, - pid = pad(process.pid.toString(36), padding), - hostname = getHostname(), - length = hostname.length, - hostId = pad(hostname - .split('') - .reduce(function (prev, char) { - return +prev + char.charCodeAt(0); - }, +length + 36) - .toString(36), - padding); - -module.exports = function fingerprint () { - return pid + hostId; -}; diff --git a/lib/esm/fingerprint.mjs b/lib/fingerprint.mjs similarity index 97% rename from lib/esm/fingerprint.mjs rename to lib/fingerprint.mjs index 8ebd9b7..5112538 100644 --- a/lib/esm/fingerprint.mjs +++ b/lib/fingerprint.mjs @@ -1,4 +1,4 @@ -import pad from './pad.mjs'; +import pad from './pad'; import os from 'os'; function getHostname () { diff --git a/lib/fingerprint.react-native.js b/lib/fingerprint.react-native.mjs similarity index 58% rename from lib/fingerprint.react-native.js rename to lib/fingerprint.react-native.mjs index e6bb642..9668233 100644 --- a/lib/fingerprint.react-native.js +++ b/lib/fingerprint.react-native.mjs @@ -1,8 +1,8 @@ -var pad = require('./pad.js'); +import pad from './pad'; var globalCount = Object.keys(global); var clientId = pad(globalCount.toString(36), 4); -module.exports = function fingerprint () { +export default function fingerprint () { return clientId; -}; +} diff --git a/lib/is-cuid.js b/lib/is-cuid.js deleted file mode 100644 index db7d8f4..0000000 --- a/lib/is-cuid.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Check the provided value is a valid device id - * @param {unknown} value - * @returns - */ -module.exports = function isCuid (value) { - return typeof value === 'string' && (/^c[a-z0-9]{20,32}$/).test(value); -}; diff --git a/lib/esm/is-cuid.mjs b/lib/is-cuid.mjs similarity index 100% rename from lib/esm/is-cuid.mjs rename to lib/is-cuid.mjs diff --git a/lib/pad.js b/lib/pad.js deleted file mode 100644 index cba5716..0000000 --- a/lib/pad.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = function pad (num, size) { - var s = '000000000' + num; - return s.substr(s.length - size); -}; diff --git a/lib/esm/pad.mjs b/lib/pad.mjs similarity index 100% rename from lib/esm/pad.mjs rename to lib/pad.mjs diff --git a/package.json b/package.json index 92e9129..1a54592 100644 --- a/package.json +++ b/package.json @@ -6,30 +6,31 @@ "name": "Bugsnag", "url": "https://bugsnag.com" }, - "main": "index.js", + "main": "dist/index.js", "types": "cuid.d.ts", "exports": { ".": { - "import": "./index.mjs", - "default": "./index.js" + "import": "./index.esm.mjs", + "require": "./dist/index.js" }, "./lib/fingerprint": { - "browser": "./lib/esm/fingerprint.browser.mjs", - "node": "./lib/esm/fingerprint.mjs", - "default": "./lib/fingerprint.js" + "browser": "./lib/fingerprint.browser.mjs", + "node": "./lib/fingerprint.mjs", + "default": "./dist/lib/fingerprint.js" } }, "imports": { "#fingerprint": { - "browser": "./lib/esm/fingerprint.browser.mjs", - "default": "./lib/esm/fingerprint.mjs" + "browser": "./lib/fingerprint.browser.mjs", + "node": "./lib/fingerprint.mjs", + "default": "./lib/fingerprint.react-native.js" } }, "browser": { - "./lib/fingerprint.js": "./lib/fingerprint.browser.js" + "./dist/lib/fingerprint.js": "./dist/lib/fingerprint.browser.js" }, "react-native": { - "./lib/fingerprint.js": "./lib/fingerprint.react-native.js" + "./dist/lib/fingerprint.js": "./dist/lib/fingerprint.react-native.js" }, "keywords": [ "id", @@ -44,12 +45,13 @@ }, "files": [ "lib", + "dist", "cuid.d.ts", - "index.js", - "index.mjs" + "index.esm.mjs" ], "license": "MIT", "devDependencies": { + "@rollup/plugin-alias": "^5.1.1", "browserify": "17.0.0", "eslint": "^8.49", "jasmine": "^2.5.0", @@ -59,13 +61,15 @@ "karma-chrome-launcher": "^2.2.0", "karma-jasmine": "^1.1.1", "mkdirp": "^0.5.1", + "rollup": "^4.34.2", "uglify-js": "^2.7.4", "watchify": "^4.0.0", "webworkify": "^1.4.0" }, "scripts": { - "build": "mkdirp dist && browserify index.js -s cuid -o dist/cuid.js && uglifyjs dist/cuid.js -c -m -o dist/cuid.min.js", - "lint": "eslint index.js lib test", + "prebuild": "rm -rf dist", + "build": "rollup -c", + "lint": "eslint index.mjs lib test", "test": "npm run lint && npm run test:server && npm run test:browser", "test:browser": "karma start test/karma.conf.js", "test:server": "jasmine test/**/*.js", diff --git a/rollup.config.mjs b/rollup.config.mjs new file mode 100644 index 0000000..36e2996 --- /dev/null +++ b/rollup.config.mjs @@ -0,0 +1,30 @@ +const sharedOutput = { + format: 'cjs', + dir: 'dist', + preserveModules: true, + generatedCode: { + preset: 'es2015' + } +}; + +export default [ + { + external: ['os'], + input: 'index.mjs', + output: sharedOutput + }, + { + input: 'lib/fingerprint.browser.mjs', + output: { + ...sharedOutput, + dir: 'dist/lib' + } + }, + { + input: 'lib/fingerprint.react-native.mjs', + output: { + ...sharedOutput, + dir: 'dist/lib' + } + } +]; From 2b2523cd2416b73525430c9cbef21e73859e058e Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 4 Feb 2025 15:57:37 +0000 Subject: [PATCH 3/7] use earlier version of rollup --- package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index 1a54592..bf408ff 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "license": "MIT", "devDependencies": { "@rollup/plugin-alias": "^5.1.1", - "browserify": "17.0.0", "eslint": "^8.49", "jasmine": "^2.5.0", "jasmine-core": "^2.8.0", @@ -61,8 +60,7 @@ "karma-chrome-launcher": "^2.2.0", "karma-jasmine": "^1.1.1", "mkdirp": "^0.5.1", - "rollup": "^4.34.2", - "uglify-js": "^2.7.4", + "rollup": "^3.19.1", "watchify": "^4.0.0", "webworkify": "^1.4.0" }, From d7b343c6f731ba9ad65ce503c056a599ea9f1771 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 4 Feb 2025 16:07:25 +0000 Subject: [PATCH 4/7] build using rollup v2 --- package.json | 14 +++++++------- rollup.config.mjs | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index bf408ff..b776115 100644 --- a/package.json +++ b/package.json @@ -6,31 +6,31 @@ "name": "Bugsnag", "url": "https://bugsnag.com" }, - "main": "dist/index.js", + "main": "dist/index.mjs", "types": "cuid.d.ts", "exports": { ".": { "import": "./index.esm.mjs", - "require": "./dist/index.js" + "require": "./dist/index.mjs" }, "./lib/fingerprint": { "browser": "./lib/fingerprint.browser.mjs", "node": "./lib/fingerprint.mjs", - "default": "./dist/lib/fingerprint.js" + "default": "./dist/lib/fingerprint.mjs" } }, "imports": { "#fingerprint": { "browser": "./lib/fingerprint.browser.mjs", "node": "./lib/fingerprint.mjs", - "default": "./lib/fingerprint.react-native.js" + "default": "./lib/fingerprint.react-native.mjs" } }, "browser": { - "./dist/lib/fingerprint.js": "./dist/lib/fingerprint.browser.js" + "./dist/lib/fingerprint.mjs": "./dist/lib/fingerprint.browser.mjs" }, "react-native": { - "./dist/lib/fingerprint.js": "./dist/lib/fingerprint.react-native.js" + "./dist/lib/fingerprint.mjs": "./dist/lib/fingerprint.react-native.mjs" }, "keywords": [ "id", @@ -60,7 +60,7 @@ "karma-chrome-launcher": "^2.2.0", "karma-jasmine": "^1.1.1", "mkdirp": "^0.5.1", - "rollup": "^3.19.1", + "rollup": "^2.76.0", "watchify": "^4.0.0", "webworkify": "^1.4.0" }, diff --git a/rollup.config.mjs b/rollup.config.mjs index 36e2996..da79a16 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -2,6 +2,7 @@ const sharedOutput = { format: 'cjs', dir: 'dist', preserveModules: true, + exports: 'default', generatedCode: { preset: 'es2015' } From 358333d5b12f180a57f23ad17d5376bf0340d9d9 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 4 Feb 2025 16:19:57 +0000 Subject: [PATCH 5/7] remove unnecessary file extensions --- index.esm.mjs => index.esm.js | 0 index.mjs => index.js | 0 lib/{cuid.mjs => cuid.js} | 0 ...int.browser.mjs => fingerprint.browser.js} | 0 lib/{fingerprint.mjs => fingerprint.js} | 0 ...native.mjs => fingerprint.react-native.js} | 0 lib/{is-cuid.mjs => is-cuid.js} | 0 lib/{pad.mjs => pad.js} | 0 package.json | 24 +++++++++---------- rollup.config.mjs | 6 ++--- 10 files changed, 14 insertions(+), 16 deletions(-) rename index.esm.mjs => index.esm.js (100%) rename index.mjs => index.js (100%) rename lib/{cuid.mjs => cuid.js} (100%) rename lib/{fingerprint.browser.mjs => fingerprint.browser.js} (100%) rename lib/{fingerprint.mjs => fingerprint.js} (100%) rename lib/{fingerprint.react-native.mjs => fingerprint.react-native.js} (100%) rename lib/{is-cuid.mjs => is-cuid.js} (100%) rename lib/{pad.mjs => pad.js} (100%) diff --git a/index.esm.mjs b/index.esm.js similarity index 100% rename from index.esm.mjs rename to index.esm.js diff --git a/index.mjs b/index.js similarity index 100% rename from index.mjs rename to index.js diff --git a/lib/cuid.mjs b/lib/cuid.js similarity index 100% rename from lib/cuid.mjs rename to lib/cuid.js diff --git a/lib/fingerprint.browser.mjs b/lib/fingerprint.browser.js similarity index 100% rename from lib/fingerprint.browser.mjs rename to lib/fingerprint.browser.js diff --git a/lib/fingerprint.mjs b/lib/fingerprint.js similarity index 100% rename from lib/fingerprint.mjs rename to lib/fingerprint.js diff --git a/lib/fingerprint.react-native.mjs b/lib/fingerprint.react-native.js similarity index 100% rename from lib/fingerprint.react-native.mjs rename to lib/fingerprint.react-native.js diff --git a/lib/is-cuid.mjs b/lib/is-cuid.js similarity index 100% rename from lib/is-cuid.mjs rename to lib/is-cuid.js diff --git a/lib/pad.mjs b/lib/pad.js similarity index 100% rename from lib/pad.mjs rename to lib/pad.js diff --git a/package.json b/package.json index b776115..5d3855f 100644 --- a/package.json +++ b/package.json @@ -6,31 +6,29 @@ "name": "Bugsnag", "url": "https://bugsnag.com" }, - "main": "dist/index.mjs", + "main": "dist/index.js", "types": "cuid.d.ts", "exports": { ".": { - "import": "./index.esm.mjs", - "require": "./dist/index.mjs" + "import": "./index.esm.js", + "require": "./dist/index.js" }, "./lib/fingerprint": { - "browser": "./lib/fingerprint.browser.mjs", - "node": "./lib/fingerprint.mjs", - "default": "./dist/lib/fingerprint.mjs" + "browser": "./lib/fingerprint.browser.js", + "node": "./lib/fingerprint.js" } }, "imports": { "#fingerprint": { - "browser": "./lib/fingerprint.browser.mjs", - "node": "./lib/fingerprint.mjs", - "default": "./lib/fingerprint.react-native.mjs" + "browser": "./lib/fingerprint.browser.js", + "node": "./lib/fingerprint.js" } }, "browser": { - "./dist/lib/fingerprint.mjs": "./dist/lib/fingerprint.browser.mjs" + "./dist/lib/fingerprint.js": "./dist/lib/fingerprint.browser.js" }, "react-native": { - "./dist/lib/fingerprint.mjs": "./dist/lib/fingerprint.react-native.mjs" + "./dist/lib/fingerprint.js": "./dist/lib/fingerprint.react-native.js" }, "keywords": [ "id", @@ -47,7 +45,7 @@ "lib", "dist", "cuid.d.ts", - "index.esm.mjs" + "index.esm.js" ], "license": "MIT", "devDependencies": { @@ -67,7 +65,7 @@ "scripts": { "prebuild": "rm -rf dist", "build": "rollup -c", - "lint": "eslint index.mjs lib test", + "lint": "eslint index.js lib test", "test": "npm run lint && npm run test:server && npm run test:browser", "test:browser": "karma start test/karma.conf.js", "test:server": "jasmine test/**/*.js", diff --git a/rollup.config.mjs b/rollup.config.mjs index da79a16..efc0c0d 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -11,18 +11,18 @@ const sharedOutput = { export default [ { external: ['os'], - input: 'index.mjs', + input: 'index.js', output: sharedOutput }, { - input: 'lib/fingerprint.browser.mjs', + input: 'lib/fingerprint.browser.js', output: { ...sharedOutput, dir: 'dist/lib' } }, { - input: 'lib/fingerprint.react-native.mjs', + input: 'lib/fingerprint.react-native.js', output: { ...sharedOutput, dir: 'dist/lib' From 41370062c472c334bd949558c91498bad5bdc326 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 18 Feb 2025 10:14:23 +0000 Subject: [PATCH 6/7] refactor: :recycle: include fingerprint and isCuid named exports --- index.esm.js | 3 +++ index.js | 3 +++ test/test.js | 12 ++++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/index.esm.js b/index.esm.js index 1fa70eb..efa197b 100644 --- a/index.esm.js +++ b/index.esm.js @@ -12,7 +12,10 @@ import fingerprint from '#fingerprint'; import createCuid from './lib/cuid'; +import isCuid from './lib/is-cuid'; const cuid = createCuid(fingerprint); +export { fingerprint, isCuid }; + export default cuid; diff --git a/index.js b/index.js index 4e16a47..6b6dd7a 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,10 @@ import fingerprint from './lib/fingerprint'; import createCuid from './lib/cuid'; +import isCuid from './lib/is-cuid'; const cuid = createCuid(fingerprint); +export { fingerprint, isCuid }; + export default cuid; diff --git a/test/test.js b/test/test.js index 83197af..5f489c0 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,6 @@ /* global describe, it, expect, pending */ -var cuid = require('../'); +var cuid, { isCuid } = require('../'); // browser check function adapted from is-in-browser module var isInBrowser = typeof window === 'object' && @@ -49,19 +49,19 @@ describe('cuid collisions', function () { describe('isCuid()', function () { it('should return true for generated cuids', function () { - expect(cuid.isCuid(cuid())).toBe(true); + expect(isCuid(cuid())).toBe(true); }); it('should return false for strings that are too short', function () { - expect(cuid.isCuid('cuidistooshort')).toBe(false); + expect(isCuid('cuidistooshort')).toBe(false); }); it('should return false for strings that are too long', function () { - expect(cuid.isCuid('cuidismorethan32characterssoisnotvalid')).toBe(false); + expect(isCuid('cuidismorethan32characterssoisnotvalid')).toBe(false); }); it('should return false for strings that do not match the format', function () { - expect(cuid.isCuid('doesnotbeginwithacsoisinvalid')).toBe(false); - expect(cuid.isCuid('contains-invalid-characters')).toBe(false); + expect(isCuid('doesnotbeginwithacsoisinvalid')).toBe(false); + expect(isCuid('contains-invalid-characters')).toBe(false); }); }); From 5bd7b9ae24169aa20ae066b8e37764bb77cdfff9 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 18 Feb 2025 10:21:13 +0000 Subject: [PATCH 7/7] only include named exports for esmodules --- index.js | 3 --- test/test.js | 12 ++++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 6b6dd7a..4e16a47 100644 --- a/index.js +++ b/index.js @@ -12,10 +12,7 @@ import fingerprint from './lib/fingerprint'; import createCuid from './lib/cuid'; -import isCuid from './lib/is-cuid'; const cuid = createCuid(fingerprint); -export { fingerprint, isCuid }; - export default cuid; diff --git a/test/test.js b/test/test.js index 5f489c0..83197af 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,6 @@ /* global describe, it, expect, pending */ -var cuid, { isCuid } = require('../'); +var cuid = require('../'); // browser check function adapted from is-in-browser module var isInBrowser = typeof window === 'object' && @@ -49,19 +49,19 @@ describe('cuid collisions', function () { describe('isCuid()', function () { it('should return true for generated cuids', function () { - expect(isCuid(cuid())).toBe(true); + expect(cuid.isCuid(cuid())).toBe(true); }); it('should return false for strings that are too short', function () { - expect(isCuid('cuidistooshort')).toBe(false); + expect(cuid.isCuid('cuidistooshort')).toBe(false); }); it('should return false for strings that are too long', function () { - expect(isCuid('cuidismorethan32characterssoisnotvalid')).toBe(false); + expect(cuid.isCuid('cuidismorethan32characterssoisnotvalid')).toBe(false); }); it('should return false for strings that do not match the format', function () { - expect(isCuid('doesnotbeginwithacsoisinvalid')).toBe(false); - expect(isCuid('contains-invalid-characters')).toBe(false); + expect(cuid.isCuid('doesnotbeginwithacsoisinvalid')).toBe(false); + expect(cuid.isCuid('contains-invalid-characters')).toBe(false); }); });