Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5152): remove harmful/unecessary RequireRewriter rollup transformer #568

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 0 additions & 44 deletions etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs

This file was deleted.

3 changes: 1 addition & 2 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 1 addition & 15 deletions src/utils/node_byte_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down