Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(response): added support to Uint8Array #540

Merged
merged 1 commit into from
Jul 25, 2022

Conversation

H4ad
Copy link
Contributor

@H4ad H4ad commented Jul 22, 2022

Issue #, if available:
#539

Description of changes:

Added support for checking if the data is Uint8Array because Next.js returns it in ssr.

About implementation

@brett-vendia I tried to add test to this new feature but I couldn't find anywhere to put it or how to test properly,
I added a test, but in the coverage I didn't see the new validation being marked as tested, any thoughts?

I also added support inside the getString function, in this dougmoscrop/serverless-http#232 the author didn't add it, but to me it makes sense that the function also has support for Uint8Array.

Checklist

  • Tests have been added and are passing
  • Documentation has been updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

__tests__/integration.js Outdated Show resolved Hide resolved
res.set('X-Custom-Header', singleNormal)

const readStream = new stream.PassThrough()
const buffer = Uint8Array.from(
Copy link

Choose a reason for hiding this comment

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

You wrote in this PR’s description:

I added a test, but in the coverage I didn't see the new validation being marked as tested, any thoughts?

In your __tests__/integration.js file, Buffer.isBuffer(buffer) returns false,
and buffer instanceof Uint8Array returns true, both as expected.

However, when I print Buffer.isBuffer(buffer) and buffer instanceof Uint8Array in addData, both return true 😲

data is passed to addData in write: (data, encoding, cb) => {, but I don't know how where and why data suddenly makes Buffer.isBuffer return true.

Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've seen this behavior of buffer instanceof Uint8Array covering cases of any buffer happening in my library too, Webstorm also tells me to remove data instanceof Uint8Array and leave just Buffer.isBuffer(data) but my tests failed when I passed Uint8Array, so you need to check this instanceof.

Well, maybe it's the internal behavior of express that makes this new validation not marked as tested, should we leave it as is or try to mark it in the test?

Copy link

Choose a reason for hiding this comment

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

Yeah, it looks like this issue comes from special behaviors of express and Next.js.
The original issue I posted mentioned Next.js (and the issue also happens in our Next.js apps), so if we wanted to have this particular case covered by tests, we would need to somehow replace express by Next.js (or some other app), which does not convert Uint8Array to Buffer.

So the test you added makes sense if the if (Buffer.isBuffer(data) case has not been tested elsewhere.
If the added test is only for covering || data instanceof Uint8Array, I personally would remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I reverse the validation to check the Uint8Array first, the validation is marked as tested:

image

But if I remove my test, the validation will still be marked as tested, so I'll remove my test because it's not actually testing anything.

Let's wait for Brett to see if he has any ideas on how to properly test this line.

@H4ad H4ad force-pushed the feature/nextjs-ssr branch from ae54544 to aa1b321 Compare July 22, 2022 11:00
@H4ad H4ad force-pushed the feature/nextjs-ssr branch from aa1b321 to 19037fa Compare July 22, 2022 13:50
@brettstack brettstack merged commit 8b76331 into CodeGenieApp:mainline Jul 25, 2022
github-actions bot pushed a commit that referenced this pull request Jul 25, 2022
# [4.10.0](v4.9.0...v4.10.0) (2022-07-25)

### Features

* **response:** added support to Uint8Array ([#540](#540)) ([8b76331](8b76331))
@github-actions
Copy link

🎉 This PR is included in version 4.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@H4ad H4ad deleted the feature/nextjs-ssr branch July 25, 2022 11:32
OneDev0411 added a commit to OneDev0411/serverless-express that referenced this pull request Mar 16, 2023
# [4.10.0](CodeGenieApp/serverless-express@v4.9.0...v4.10.0) (2022-07-25)

### Features

* **response:** added support to Uint8Array ([#540](CodeGenieApp/serverless-express#540)) ([8b76331](CodeGenieApp/serverless-express@8b76331))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants