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

[core-rest-pipeline] Fix handling of typed arrays in request bodies #15904

Merged
merged 1 commit into from
Jun 23, 2021

Conversation

xirzec
Copy link
Member

@xirzec xirzec commented Jun 22, 2021

NodeJS does not support directly passing typed arrays or ArrayBuffers to http.ClientRequest streams. This means we must correctly wrap these types in a Buffer for them to be serialized correctly.

NodeJS does not support directly passing typed arrays or ArrayBuffers to `http.ClientRequest` streams. This means we must correctly wrap these types in a `Buffer` for them to be serialized correctly.
@xirzec xirzec added Client This issue points to a problem in the data-plane of the library. Azure.Core labels Jun 22, 2021
@xirzec xirzec self-assigned this Jun 22, 2021
} else if (isArrayBuffer(body)) {
req.end(ArrayBuffer.isView(body) ? Buffer.from(body.buffer) : Buffer.from(body));
} else {
logger.error("Unrecognized body type", body);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is basically a "never" situation since:

  • FormData would be handled by the isReadableStream case above
  • Blob is browser only

There are no other supported body types after that.

Copy link
Member

Choose a reason for hiding this comment

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

How is FormData handled by isReadableStream? It doesn't have pipe function. also I had thought it's browser only (go to definition opens lib.dom.d.ts) and I didn't find it in nodejs docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

FormData (the type you saw) gets converted by this policy when in Node:

It uses an npm package form-data to create another object type (confusingly also called FormData) that implements the Stream interface.

Copy link
Member

@maorleger maorleger left a comment

Choose a reason for hiding this comment

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

🚀

Copy link
Member

@deyaaeldeen deyaaeldeen 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 the fix! I did not know about typed arrays until now, found this to be a useful intro: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

As you might expect, they're useful for code that is performance-critical since they should have compact memory representation and operations on them should be fast.

@xirzec xirzec merged commit 137c671 into Azure:main Jun 23, 2021
@xirzec xirzec deleted the fixArrayBufferCoreV2 branch June 23, 2021 20:28
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-js that referenced this pull request Sep 3, 2021
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-js that referenced this pull request Sep 3, 2021
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-js that referenced this pull request Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants