Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


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

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@
"prepare": "node etc/prepare.js",
"release": "standard-version -i HISTORY.md"
}
}
}
4 changes: 1 addition & 3 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';
import { RequireVendor } from './etc/rollup/rollup-plugin-require-vendor/require_vendor.mjs';

/** @type {typescript.RollupTypescriptOptions} */
Expand Down Expand Up @@ -57,7 +56,6 @@ const config = [
input,
plugins: [
typescript(tsConfig),
requireRewriter({ target: 'browser' }),
nodeResolve({ resolveOnly: [] })
],
output: {
Expand All @@ -68,7 +66,7 @@ const config = [
},
{
input,
plugins: [typescript(tsConfig), requireRewriter({ target: 'node' }), nodeResolve({ resolveOnly: [] })],
plugins: [typescript(tsConfig), nodeResolve({ resolveOnly: [] })],
output: {
file: 'lib/bson.node.mjs',
format: 'esm',
Expand Down
35 changes: 14 additions & 21 deletions src/utils/node_byte_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,27 @@ type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists
// Node.js global
declare const Buffer: NodeJsBufferConstructor;
declare const require: (mod: 'crypto') => { randomBytes: (byteLength: number) => Uint8Array };

/** @internal */
export function nodejsMathRandomBytes(byteLength: number) {
function nodejsMathRandomBytes(byteLength: number): NodeJsBuffer {
return nodeJsByteUtils.fromNumberArray(
Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
);
}

/**
* @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.
*/
const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => {
try {
return require('crypto').randomBytes;
} catch {
/** @internal */
function nodejsSecureRandomBytes(byteLength: number): NodeJsBuffer {
// @ts-expect-error: crypto.getRandomValues cannot actually be null here
return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
}

const nodejsRandomBytes = (() => {
const { crypto } = globalThis as {
crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array };
};
if (crypto != null && typeof crypto.getRandomValues === 'function') {
return nodejsSecureRandomBytes;
} else {
return nodejsMathRandomBytes;
}
})();
Expand Down