diff --git a/rollup.config.common.js b/rollup.config.common.js index f9e4667..0207654 100644 --- a/rollup.config.common.js +++ b/rollup.config.common.js @@ -16,12 +16,7 @@ export function createUMDConfig(merger) { format: 'umd', name: 'Skedify', file: PACKAGE.main, - - globals: { - crypto: 'crypto', - }, }, - external: ['crypto'], plugins: [ alias({ resolve: ['.js', ''], diff --git a/src/util/createToken.browser.test.js b/src/util/createToken.browser.test.js deleted file mode 100644 index 12a5808..0000000 --- a/src/util/createToken.browser.test.js +++ /dev/null @@ -1,33 +0,0 @@ -import crypto from 'crypto' - -describe('createToken', () => { - beforeAll(() => { - // I know we "fake" the browser implementation, however this way - // we are still testing the browser detection itself. - crypto.getRandomValues = (buffer) => crypto.randomFillSync(buffer) - }) - - afterAll(() => { - // Remove our fake implementation again - //eslint-disable-next-line better/no-deletes - delete crypto.getRandomValues - }) - - it('should generate a random string', () => { - // We are only importing it now because otherwise our `getRandomValues` implementation - // won't get pucked up correctly. - // It's not super clean but it works for now. - const createToken = require('./createToken').default //eslint-disable-line global-require - - expect(createToken()).toBeDefined() - }) - - it('should remove all dashes from the uuid', () => { - // We are only importing it now because otherwise our `getRandomValues` implementation - // won't get pucked up correctly. - // It's not super clean but it works for now. - const createToken = require('./createToken').default //eslint-disable-line global-require - - expect(createToken()).not.toContain('-') - }) -}) diff --git a/src/util/createToken.js b/src/util/createToken.js index 8a8808b..31f331c 100644 --- a/src/util/createToken.js +++ b/src/util/createToken.js @@ -1,5 +1,3 @@ -import crypto from 'crypto' - /** * This code is copied from: * - https://www.npmjs.com/package/uuid @@ -24,27 +22,32 @@ import crypto from 'crypto' const rnds8 = new Uint8Array(16) -const getRandomValues = - // eslint-disable-next-line better/no-typeofs - (typeof crypto !== 'undefined' && - crypto.getRandomValues && - crypto.getRandomValues.bind(crypto)) || - // eslint-disable-next-line better/no-typeofs - (typeof msCrypto !== 'undefined' && - // eslint-disable-next-line better/no-typeofs,no-undef - /* istanbul ignore next */ typeof msCrypto.getRandomValues === 'function' && - // eslint-disable-next-line no-undef - /* istanbul ignore next */ msCrypto.getRandomValues.bind(msCrypto)) +/* eslint-disable better/no-typeofs,no-undef */ +const IS_NODE_ENVIRONMENT = + typeof navigator === 'undefined' || navigator.userAgent.indexOf('jsdom') >= 0 +// If JSDom is detected then we are in a testing environment +// We want this to count as a Node environment as well + +/* istanbul ignore next */ function rng() { - if (getRandomValues) { - // For Browsers - return getRandomValues(rnds8) + if (IS_NODE_ENVIRONMENT) { + // If we're here, we're in a NodeJS environment + return crypto.randomFillSync(rnds8) } - // If we're here, we're in a NodeJS environment - return crypto.randomFillSync(rnds8) + // For Browsers, including IE11 + const getRandomValues = + (typeof crypto !== 'undefined' && + crypto.getRandomValues && + crypto.getRandomValues.bind(crypto)) || + (typeof msCrypto !== 'undefined' && + typeof msCrypto.getRandomValues === 'function' && + msCrypto.getRandomValues.bind(msCrypto)) + + return getRandomValues(rnds8) } +/* eslint-enable better/no-typeofs,no-undef */ function validate(uuid) { const REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i diff --git a/src/util/createToken.node.test.js b/src/util/createToken.test.js similarity index 100% rename from src/util/createToken.node.test.js rename to src/util/createToken.test.js