fix(mongo): fix support for deno by handling TypedArray when cloning#7258
fix(mongo): fix support for deno by handling TypedArray when cloning#7258B4nan merged 1 commit intomikro-orm:masterfrom
TypedArray when cloning#7258Conversation
| return null; | ||
| } | ||
|
|
||
| const TypedArray = Object.getPrototypeOf(Uint8Array); |
There was a problem hiding this comment.
We can retrieve the TypedArray prototype by getting the prototype of any TypedArray subtype, like Uint8Array (see MDN).
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
a87364f to
5ede55c
Compare
|
huh, did I forget to add formatting check to the CI? please run edit: and of course, thanks! |
de9e679 to
2c75491
Compare
2c75491 to
9bed5af
Compare
|
@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 Thanks for the review! |
TypedArray when cloning
| parent.copy(child as Buffer); | ||
| return child; | ||
| } else if (parent instanceof TypedArray) { | ||
| child = parent.copyWithin(0); |
There was a problem hiding this comment.
claude flags this as wrong, since it doesnt actually clone anything. his suggestion is this instead:
| 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
There was a problem hiding this comment.
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!
This PR fixes the usage of
@mikro-orm/mongodbwith 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:Investigation
Why is this bug appearing with Deno? The behavior is different because of this line of code in the
bsonpackage:Buffer.prototype?._isBufferisundefined; soByteUtilsis usingnodeJsByteUtilsBuffer.prototype?._isBufferistrue; soByteUtilsis usingwebByteUtilsThe content of the
fromHexmethod is different in both implementations:nodeJsByteUtils, it usesBuffer.fromwebByteUtils, it usesUint8Array.fromThe issue is that our
clonefunction inpackages/core/src/utils/clone.tsdoes not handle cloning ofUint8Arrayproperly.For instance, whit the following
Uint8Arrayarray:Cloning it with our util function will result in:
Fix
In this PR, we add proper support for cloning all
TypedArraysubtypes.