Skip to content

fix(mongo): fix support for deno by handling TypedArray when cloning#7258

Merged
B4nan merged 1 commit intomikro-orm:masterfrom
alexandre-abrioux:deno-mongodb
Mar 7, 2026
Merged

fix(mongo): fix support for deno by handling TypedArray when cloning#7258
B4nan merged 1 commit intomikro-orm:masterfrom
alexandre-abrioux:deno-mongodb

Conversation

@alexandre-abrioux
Copy link
Contributor

@alexandre-abrioux alexandre-abrioux commented Mar 7, 2026

This PR fixes the usage of @mikro-orm/mongodb with Deno.

Issue

Using the newly added reproduction test case in tests/deno/mongodb.test.ts, and running it with no additional code change results in the following error:

> deno run --allow-read --allow-write --allow-env --allow-net --allow-sys --node-modules-dir=manual tests/deno/mongodb.test.ts
error: Uncaught (in promise) TypeError: this is not a typed array.
    at Uint8Array.values (<anonymous>)
    at Array.from (<anonymous>)
    at Object.toHex (file:///.../mikro-orm/node_modules/bson/lib/bson.cjs:481:22)
    at ObjectId.toHexString (file:///.../mikro-orm/node_modules/bson/lib/bson.cjs:2672:37)
    at eval (eval at createFunction (file:///.../mikro-orm/packages/core/src/utils/Utils.ts:900:14), <anonymous>:13:68)
    at ChangeSetComputer.computePayload (file:///.../mikro-orm/packages/core/src/unit-of-work/ChangeSetComputer.ts:145:20)
    at ChangeSetComputer.computeChangeSet (file:///.../mikro-orm/packages/core/src/unit-of-work/ChangeSetComputer.ts:61:56)
    at UnitOfWork.findNewEntities (file:///.../mikro-orm/packages/core/src/unit-of-work/UnitOfWork.ts:780:46)
    at UnitOfWork.computeChangeSets (file:///.../mikro-orm/packages/core/src/unit-of-work/UnitOfWork.ts:634:12)
    at UnitOfWork.doCommit (file:///.../mikro-orm/packages/core/src/unit-of-work/UnitOfWork.ts:514:12)

Investigation

Why is this bug appearing with Deno? The behavior is different because of this line of code in the bson package:

  • with Node.js, Buffer.prototype?._isBuffer is undefined ; so ByteUtils is using nodeJsByteUtils
  • with Deno, Buffer.prototype?._isBuffer is true ; so ByteUtils is using webByteUtils

The content of the fromHex method is different in both implementations:

The issue is that our clone function in packages/core/src/utils/clone.ts does not handle cloning of Uint8Array properly.

For instance, whit the following Uint8Array array:

Uint8Array(12) [
  105, 172, 10
]

Cloning it with our util function will result in:

Uint8Array {
  "0": 105,
  "1": 172,
  "2": 10
}

Fix

In this PR, we add proper support for cloning all TypedArray subtypes.

return null;
}

const TypedArray = Object.getPrototypeOf(Uint8Array);
Copy link
Contributor Author

@alexandre-abrioux alexandre-abrioux Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can retrieve the TypedArray prototype by getting the prototype of any TypedArray subtype, like Uint8Array (see MDN).

@codecov
Copy link

codecov bot commented Mar 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.68%. Comparing base (cc4b022) to head (9bed5af).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7258   +/-   ##
=======================================
  Coverage   99.68%   99.68%           
=======================================
  Files         261      261           
  Lines       25064    25068    +4     
  Branches     6967     6969    +2     
=======================================
+ Hits        24984    24988    +4     
  Misses         77       77           
  Partials        3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@B4nan
Copy link
Member

B4nan commented Mar 7, 2026

huh, did I forget to add formatting check to the CI?

please run yarn format

edit: and of course, thanks!

@alexandre-abrioux alexandre-abrioux marked this pull request as ready for review March 7, 2026 13:33
@alexandre-abrioux alexandre-abrioux force-pushed the deno-mongodb branch 3 times, most recently from de9e679 to 2c75491 Compare March 7, 2026 13:44
@alexandre-abrioux
Copy link
Contributor Author

@B4nan Sorry I missed that! Done ✅

I've also added CI support for the new Deno test. I've tried to make it as light as possible by only pulling and starting the mongo service.

Thanks for the review!

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks again!

@B4nan B4nan changed the title fix(core/utils): clone should handle TypedArray fix(mongo): fix support for deno by handling TypedArray when cloning Mar 7, 2026
@B4nan B4nan merged commit 17ec4f5 into mikro-orm:master Mar 7, 2026
23 of 24 checks passed
parent.copy(child as Buffer);
return child;
} else if (parent instanceof TypedArray) {
child = parent.copyWithin(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude flags this as wrong, since it doesnt actually clone anything. his suggestion is this instead:

Suggested change
child = parent.copyWithin(0);
child = parent.slice();

i should have paid more attention to this, copyWithin mutates, it doesnt create a new copy, any reason you used that? will address this as part of #7256

Copy link
Contributor Author

@alexandre-abrioux alexandre-abrioux Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, my bad 😬 Sorry, I don't manipulate TypedArray often, so I was looking at a method to clone it and misunderstood the docs of copyWithin. Indeed, slice is more appropriate, thanks for catching it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants