From a337da7bdcf8c0dbe2979a389b87ae3320ca61a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Mil?= Date: Fri, 10 Jun 2022 12:11:12 +0200 Subject: [PATCH 1/4] Do not require crypto in browser builds --- package-lock.json | 26 +++++++++++++++++++++++++- package.json | 1 + rollup.config.js | 37 +++++++++++++++++++++++-------------- src/parser/utils.ts | 44 +++++++++++++++++++++++++------------------- 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index c80bdcb0..27fc3c3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "bson", - "version": "4.6.3", + "version": "4.6.4", "license": "Apache-2.0", "dependencies": { "buffer": "^5.6.0" @@ -20,6 +20,7 @@ "@rollup/plugin-commonjs": "^15.0.0", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^9.0.0", + "@rollup/plugin-replace": "^4.0.0", "@rollup/plugin-typescript": "^6.0.0", "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", @@ -1945,6 +1946,19 @@ "rollup": "^1.20.0||^2.0.0" } }, + "node_modules/@rollup/plugin-replace": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-4.0.0.tgz", + "integrity": "sha512-+rumQFiaNac9y64OHtkHGmdjm7us9bo1PlbgQfdihQtuNxzjpaB064HbRnewUOggLQxVCCyINfStkgmBeQpv1g==", + "dev": true, + "dependencies": { + "@rollup/pluginutils": "^3.1.0", + "magic-string": "^0.25.7" + }, + "peerDependencies": { + "rollup": "^1.20.0 || ^2.0.0" + } + }, "node_modules/@rollup/plugin-typescript": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-6.1.0.tgz", @@ -10898,6 +10912,16 @@ "resolve": "^1.17.0" } }, + "@rollup/plugin-replace": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-4.0.0.tgz", + "integrity": "sha512-+rumQFiaNac9y64OHtkHGmdjm7us9bo1PlbgQfdihQtuNxzjpaB064HbRnewUOggLQxVCCyINfStkgmBeQpv1g==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.1.0", + "magic-string": "^0.25.7" + } + }, "@rollup/plugin-typescript": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-6.1.0.tgz", diff --git a/package.json b/package.json index b3ce5a40..61acb58b 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@rollup/plugin-commonjs": "^15.0.0", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^9.0.0", + "@rollup/plugin-replace": "^4.0.0", "@rollup/plugin-typescript": "^6.0.0", "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", diff --git a/rollup.config.js b/rollup.config.js index f87f9e80..77d03d0f 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,6 +5,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'; import { babel } from '@rollup/plugin-babel'; import typescript from '@rollup/plugin-typescript'; import nodeGlobals from 'rollup-plugin-node-globals'; +import replace from '@rollup/plugin-replace'; const tsConfig = { allowJs: false, @@ -33,18 +34,26 @@ const tsConfig = { }; const input = 'src/bson.ts'; -const plugins = [ - typescript(tsConfig), - nodeResolve({ preferBuiltins: false }), - nodeBuiltins(), - nodeGlobals(), - commonjs({ extensions: ['.js', '.ts'] }), - babel({ - babelHelpers: 'external', - plugins: ['@babel/plugin-external-helpers'], - presets: [['@babel/env', { modules: false }]] - }) -]; +const plugins = ({ browser = false }) => { + return [ + typescript(tsConfig), + nodeResolve({ preferBuiltins: false }), + nodeBuiltins(), + nodeGlobals(), + replace({ + preventAssignment: true, + values: { + 'process.browser': browser + } + }), + commonjs({ extensions: ['.js', '.ts'] }), + babel({ + babelHelpers: 'external', + plugins: ['@babel/plugin-external-helpers'], + presets: [['@babel/env', { modules: false }]] + }) + ]; +}; const external = Object.keys(pkg.dependencies || {}); @@ -60,7 +69,7 @@ module.exports = [ exports: 'named', sourcemap: true }, - plugins, + plugins: plugins({ browser: false }), external }, { @@ -88,6 +97,6 @@ module.exports = [ sourcemap: true } ], - plugins + plugins: plugins({ browser: true }) } ]; diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 2cd128ad..7f623224 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -34,32 +34,38 @@ declare let require: Function; // eslint-disable-next-line @typescript-eslint/no-explicit-any declare let global: any; declare const self: unknown; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +declare let process: any; // Used by @rollup/plugin-replace const detectRandomBytes = (): RandomBytesFunction => { - if (typeof window !== 'undefined') { - // browser crypto implementation(s) - const target = window.crypto || window.msCrypto; // allow for IE11 - if (target && target.getRandomValues) { - return size => target.getRandomValues(Buffer.alloc(size)); + if (process.browser) { + if (typeof window !== 'undefined') { + // browser crypto implementation(s) + const target = window.crypto || window.msCrypto; // allow for IE11 + if (target && target.getRandomValues) { + return size => target.getRandomValues(Buffer.alloc(size)); + } } - } - if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) { - // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global - return size => global.crypto.getRandomValues(Buffer.alloc(size)); - } + if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) { + // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global + return size => global.crypto.getRandomValues(Buffer.alloc(size)); + } - let requiredRandomBytes: RandomBytesFunction | null | undefined; - try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - requiredRandomBytes = require('crypto').randomBytes; - } catch (e) { - // keep the fallback - } + return insecureRandomBytes; + } else { + let requiredRandomBytes: RandomBytesFunction | null | undefined; + try { + // eslint-disable-next-line @typescript-eslint/no-var-requires + requiredRandomBytes = require('crypto').randomBytes; + } catch (e) { + // keep the fallback + } - // NOTE: in transpiled cases the above require might return null/undefined + // NOTE: in transpiled cases the above require might return null/undefined - return requiredRandomBytes || insecureRandomBytes; + return requiredRandomBytes || insecureRandomBytes; + } }; export const randomBytes = detectRandomBytes(); From 79030308fedeac56d6e118677f129e6bc9b0b1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Mil?= Date: Sat, 11 Jun 2022 13:23:42 +0200 Subject: [PATCH 2/4] rename process to rollupProcess --- rollup.config.js | 2 +- src/parser/utils.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 77d03d0f..9ccd5985 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -43,7 +43,7 @@ const plugins = ({ browser = false }) => { replace({ preventAssignment: true, values: { - 'process.browser': browser + 'rollupProcess.browser': browser } }), commonjs({ extensions: ['.js', '.ts'] }), diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 7f623224..b4fc51c1 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -35,10 +35,10 @@ declare let require: Function; declare let global: any; declare const self: unknown; // eslint-disable-next-line @typescript-eslint/no-explicit-any -declare let process: any; // Used by @rollup/plugin-replace +declare let rollupProcess: any; // Used by @rollup/plugin-replace const detectRandomBytes = (): RandomBytesFunction => { - if (process.browser) { + if (rollupProcess.browser) { if (typeof window !== 'undefined') { // browser crypto implementation(s) const target = window.crypto || window.msCrypto; // allow for IE11 From bd513024f43e72e5817412072bda715f6a45abcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Mil?= Date: Sat, 11 Jun 2022 13:25:23 +0200 Subject: [PATCH 3/4] add default options to rollup plugins function --- rollup.config.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 9ccd5985..d59a8bbb 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -34,7 +34,7 @@ const tsConfig = { }; const input = 'src/bson.ts'; -const plugins = ({ browser = false }) => { +const plugins = (options = { browser: false }) => { return [ typescript(tsConfig), nodeResolve({ preferBuiltins: false }), @@ -43,7 +43,7 @@ const plugins = ({ browser = false }) => { replace({ preventAssignment: true, values: { - 'rollupProcess.browser': browser + 'rollupProcess.browser': options.browser } }), commonjs({ extensions: ['.js', '.ts'] }), @@ -69,7 +69,7 @@ module.exports = [ exports: 'named', sourcemap: true }, - plugins: plugins({ browser: false }), + plugins: plugins(), external }, { From 31c485b7f4aa8e3ebc9d0fc3c12576303b949ccd Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 14 Jun 2022 17:59:28 +0200 Subject: [PATCH 4/4] fix: revert to use process.browser --- rollup.config.js | 2 +- src/parser/utils.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index d59a8bbb..770e0f69 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -43,7 +43,7 @@ const plugins = (options = { browser: false }) => { replace({ preventAssignment: true, values: { - 'rollupProcess.browser': options.browser + 'process.browser': options.browser } }), commonjs({ extensions: ['.js', '.ts'] }), diff --git a/src/parser/utils.ts b/src/parser/utils.ts index b4fc51c1..7f623224 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -35,10 +35,10 @@ declare let require: Function; declare let global: any; declare const self: unknown; // eslint-disable-next-line @typescript-eslint/no-explicit-any -declare let rollupProcess: any; // Used by @rollup/plugin-replace +declare let process: any; // Used by @rollup/plugin-replace const detectRandomBytes = (): RandomBytesFunction => { - if (rollupProcess.browser) { + if (process.browser) { if (typeof window !== 'undefined') { // browser crypto implementation(s) const target = window.crypto || window.msCrypto; // allow for IE11