Skip to content

Conversation

sainthkh
Copy link
Contributor

@sainthkh sainthkh commented Sep 6, 2021

User facing changelog

N/A

Additional details

  • Why was this change necessary? => The patched hasBinary should work like the original

Any implementation details to explain?

The original hasBinary detects binary data inside toJSON() functions.

But the patched version doesn't do that because we don't have the toJSON argument. The obj.toJSON && typeof obj.toJSON === "function" && arguments.length === 1 in the code is always false because arguments.length is always true.

This PR solves this problem.

What is affected by this change?

I guess there are none of them. Even if binary objects are In toJSON(), it will be converted to string eventually when encoding them. So, the encoded result is the same.

It might be useful when a developer use the patched hasBinary directly in their code. But it doesn't change anything for now.

How has the user experience changed?

N/A

PR Tasks

  • Have tests been added/updated?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 6, 2021

Thanks for taking the time to open a PR!

@sainthkh sainthkh mentioned this pull request Sep 6, 2021
1 task
@sainthkh sainthkh marked this pull request as ready for review September 6, 2021 03:10
@sainthkh sainthkh requested a review from a team as a code owner September 6, 2021 03:10
@sainthkh sainthkh requested review from flotwig and kuceb and removed request for a team September 6, 2021 03:10
@sainthkh sainthkh changed the title chore: make hasBinary work more like the original chore: make hasBinary of socket.io-parser work more like the original Sep 6, 2021
@sainthkh sainthkh force-pushed the socket.io-parser-patch-fix branch from e29b808 to e02303e Compare September 8, 2021 01:08
@sainthkh
Copy link
Contributor Author

sainthkh commented Sep 8, 2021

flaky failures.

Copy link
Contributor

@flotwig flotwig left a comment

Choose a reason for hiding this comment

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

The original hasBinary detects binary data inside toJSON() functions.
But the patched version doesn't do that because we don't have the toJSON argument.

In the linked code, the toJSON parameter is unused inside of the function. Why is this change that brings the unused parameter back desirable?

@sainthkh
Copy link
Contributor Author

@flotwig

Yes, it is unused directly. But it affects the flow of the code because of the line 54, arguments.length === 1. Because there are 2 ways to call this functions: hasBinary(obj) and hasBinary(obj, true) like the line 56.

As the second argument of our patched function is an empty array by default, arguments.length is always 2 and the result of obj.toJSON && typeof obj.toJSON === "function" && arguments.length === 1 is always false. It means that our patched function doesn't check if it has binary inside the toJSON method of the obj.

(Actually, it took me a few days to understand this. I think the code is really unintuitive.)

As I said above, it doesn't affect Cypress functions. It's just done to make the patched function work like the original.

I've written a test to showcase this behavior difference.

    it('hasBinary handles binary data in toJSON()', () => {
      const x = {
        toJSON () {
          return Buffer.from('123', 'utf8')
        },
      }

      const data = ['a', x]

      expect(hasBinary(data)).to.be.true // It's true in the original, but false in the patched.
    })

@sainthkh sainthkh requested a review from flotwig September 13, 2021 23:18
Copy link
Contributor

@flotwig flotwig left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to explain. I missed the arguments.length on my first and second reads 🤦 LGTM

@flotwig flotwig merged commit 7a78c1c into cypress-io:develop Sep 14, 2021
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