From d307f41580c6821866af193903f6fba2a674cf71 Mon Sep 17 00:00:00 2001 From: ringeringeraja Date: Wed, 29 Mar 2023 11:46:05 -0300 Subject: [PATCH 1/2] fix: remove harmful/unecessary RequireRewriter rollup transformator --- .../require_rewriter.mjs | 44 ------------------- rollup.config.mjs | 3 +- 2 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs diff --git a/etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs b/etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs deleted file mode 100644 index 796ba449..00000000 --- a/etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs +++ /dev/null @@ -1,44 +0,0 @@ -import MagicString from 'magic-string'; - -const CRYPTO_IMPORT_ESM_SRC = `const nodejsRandomBytes = await (async () => { - try { - return (await import('crypto')).randomBytes;`; - -export class RequireRewriter { - /** - * Take the compiled source code input; types are expected to already have been removed - * Look for the function that depends on crypto, replace it with a top-level await - * and dynamic import for the crypto module. - * - * @param {string} code - source code of the module being transformed - * @param {string} id - module id (usually the source file name) - * @returns {{ code: string; map: import('magic-string').SourceMap }} - */ - transform(code, id) { - if (!id.includes('node_byte_utils')) { - return; - } - if (!code.includes('const nodejsRandomBytes')) { - throw new Error(`Unexpected! 'const nodejsRandomBytes' is missing from ${id}`); - } - - const start = code.indexOf('const nodejsRandomBytes'); - const endString = `return require('crypto').randomBytes;`; - const end = code.indexOf(endString) + endString.length; - - if (start < 0 || end < 0) { - throw new Error( - `Unexpected! 'const nodejsRandomBytes' or 'return require('crypto').randomBytes;' not found` - ); - } - - // MagicString lets us edit the source code and still generate an accurate source map - const magicString = new MagicString(code); - magicString.overwrite(start, end, CRYPTO_IMPORT_ESM_SRC); - - return { - code: magicString.toString(), - map: magicString.generateMap({ hires: true }) - }; - } -} diff --git a/rollup.config.mjs b/rollup.config.mjs index 8dc6fe23..aa4eb54f 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,6 +1,5 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'; import typescript from '@rollup/plugin-typescript'; -import { RequireRewriter } from './etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs'; /** @type {typescript.RollupTypescriptOptions} */ const tsConfig = { @@ -54,7 +53,7 @@ const config = [ }, { input, - plugins: [typescript(tsConfig), new RequireRewriter(), nodeResolve({ resolveOnly: [] })], + plugins: [typescript(tsConfig), nodeResolve({ resolveOnly: [] })], output: { file: 'lib/bson.mjs', format: 'esm', From e4fcc156c926bcecff8c75d2c503b09b7eab5793 Mon Sep 17 00:00:00 2001 From: ringeringeraja Date: Thu, 30 Mar 2023 17:04:21 -0300 Subject: [PATCH 2/2] chore: remove jsdoc warning on nodejsRandomBytes --- src/utils/node_byte_utils.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/utils/node_byte_utils.ts b/src/utils/node_byte_utils.ts index 468acf89..2c4a35b7 100644 --- a/src/utils/node_byte_utils.ts +++ b/src/utils/node_byte_utils.ts @@ -31,21 +31,7 @@ export function nodejsMathRandomBytes(byteLength: number) { ); } -/** - * @internal - * WARNING: REQUIRE WILL BE REWRITTEN - * - * This code is carefully used by require_rewriter.mjs any modifications must be reflected in the plugin. - * - * @remarks - * "crypto" is the only dependency BSON needs. This presents a problem for creating a bundle of the BSON library - * in an es module format that can be used both on the browser and in Node.js. In Node.js when BSON is imported as - * an es module, there will be no global require function defined, making the code below fallback to the much less desireable math.random bytes. - * In order to make our es module bundle work as expected on Node.js we need to change this `require()` to a dynamic import, and the dynamic - * import must be top-level awaited since es modules are async. So we rely on a custom rollup plugin to seek out the following lines of code - * and replace `require` with `await import` and the IIFE line (`nodejsRandomBytes = (() => { ... })()`) with `nodejsRandomBytes = await (async () => { ... })()` - * when generating an es module bundle. - */ +/** @internal */ const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => { try { return require('crypto').randomBytes;