Skip to content

Commit b125cb5

Browse files
committed
chore: write a rollup plugin to make the future of javascript possible
1 parent 556a961 commit b125cb5

File tree

18 files changed

+236
-92
lines changed

18 files changed

+236
-92
lines changed

.mocharc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"timeout": 10000,
1313
"failZero": true,
1414
"sort": true,
15-
"color": true
15+
"color": true,
16+
"node-option": [
17+
"experimental-vm-modules"
18+
]
1619
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import MagicString from 'magic-string';
2+
3+
const CRYPTO_IMPORT_ESM_SRC = `const nodejsRandomBytes = await (async () => {
4+
try {
5+
return (await import('node:crypto')).randomBytes;`;
6+
7+
class RequireRewriter {
8+
transform(code, id) {
9+
if (!id.includes('node_byte_utils')) {
10+
return;
11+
}
12+
if (!code.includes('const nodejsRandomBytes')) {
13+
throw new Error(`Unexpected! 'const nodejsRandomBytes' is missing from ${id}`);
14+
}
15+
16+
const start = code.indexOf('const nodejsRandomBytes');
17+
const endString = `return require('crypto').randomBytes;`
18+
const end = code.indexOf(endString) + endString.length;
19+
20+
if (start < 0 || end < 0) {
21+
throw new Error(
22+
`Unexpected! 'const nodejsRandomBytes' or 'return require('crypto').randomBytes;' not found`
23+
);
24+
}
25+
26+
const magicString = new MagicString(code);
27+
magicString.overwrite(start, end, CRYPTO_IMPORT_ESM_SRC);
28+
29+
return {
30+
code: magicString.toString(),
31+
map: magicString.generateMap({ hires: true })
32+
};
33+
}
34+
}
35+
36+
export default function rewriteRequire() {
37+
return new RequireRewriter();
38+
}

lib/bson.bundle.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bson.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bson.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3963,6 +3963,7 @@ var bson = /*#__PURE__*/Object.freeze({
39633963
deserialize: deserialize,
39643964
calculateObjectSize: calculateObjectSize,
39653965
deserializeStream: deserializeStream,
3966+
LongWithoutOverridesClass: LongWithoutOverridesClass,
39663967
BSONError: BSONError,
39673968
BSONTypeError: BSONTypeError,
39683969
BSONType: BSONType,
@@ -3983,6 +3984,7 @@ exports.Double = Double;
39833984
exports.EJSON = EJSON;
39843985
exports.Int32 = Int32;
39853986
exports.Long = Long;
3987+
exports.LongWithoutOverridesClass = LongWithoutOverridesClass;
39863988
exports.MaxKey = MaxKey;
39873989
exports.MinKey = MinKey;
39883990
exports.ObjectId = ObjectId;

lib/bson.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bson.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class BSONTypeError extends TypeError {
1818
function nodejsMathRandomBytes(byteLength) {
1919
return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
2020
}
21-
const nodejsRandomBytes = (() => {
21+
const nodejsRandomBytes = await (async () => {
2222
try {
23-
return require('crypto').randomBytes;
23+
return (await import('node:crypto')).randomBytes;
2424
}
2525
catch {
2626
return nodejsMathRandomBytes;
@@ -3961,11 +3961,12 @@ var bson = /*#__PURE__*/Object.freeze({
39613961
deserialize: deserialize,
39623962
calculateObjectSize: calculateObjectSize,
39633963
deserializeStream: deserializeStream,
3964+
LongWithoutOverridesClass: LongWithoutOverridesClass,
39643965
BSONError: BSONError,
39653966
BSONTypeError: BSONTypeError,
39663967
BSONType: BSONType,
39673968
EJSON: EJSON
39683969
});
39693970

3970-
export { bson as BSON, BSONError, BSONRegExp, BSONSymbol, BSONType, BSONTypeError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
3971+
export { bson as BSON, BSONError, BSONRegExp, BSONSymbol, BSONType, BSONTypeError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, LongWithoutOverridesClass, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
39713972
//# sourceMappingURL=bson.mjs.map

lib/bson.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 63 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@istanbuljs/nyc-config-typescript": "^1.0.2",
3131
"@microsoft/api-extractor": "^7.33.6",
3232
"@rollup/plugin-babel": "^6.0.2",
33-
"@rollup/plugin-commonjs": "^23.0.2",
33+
"@rollup/plugin-commonjs": "^23.0.4",
3434
"@rollup/plugin-json": "^5.0.1",
3535
"@rollup/plugin-node-resolve": "^15.0.1",
3636
"@rollup/plugin-replace": "^5.0.1",
@@ -49,6 +49,7 @@
4949
"eslint-config-prettier": "^8.5.0",
5050
"eslint-plugin-prettier": "^4.2.1",
5151
"eslint-plugin-tsdoc": "^0.2.17",
52+
"magic-string": "^0.27.0",
5253
"mocha": "10.1.0",
5354
"node-fetch": "^3.2.10",
5455
"nyc": "^15.1.0",

0 commit comments

Comments
 (0)