Files
Harsha NalluruandGitHub b939c312e9 [Document Intelligence] Corrupted images (#31908)
## Background
We encode the response Buffer to "utf8" by default in the
`NodeFetchClient` at `@azure/core-rest-pipeline`.
"utf8" encoding of Buffer has been found to corrupt the output in
certain cases, such as when dealing with PNG files (`image/png`
content-type response).
```js
Buffer.concat(buffer).toString("utf8")
```
This issue arises because "utf8" encoding is not suitable for binary
data like images, which leads to corrupted files and unexpected behavior
in applications that rely on this data.


## What changed?
To address this issue, I've implemented the following changes

- A new function `streamToUint8Array` which takes a
`NodeJS.ReadableStream` as input and returns a `Promise<Uint8Array>`.
- Users now have to call `.asNodeStream()` on the response object to get
the response stream and then pass it to `streamToUint8Array` to get the
Uint8Array.

```js
// Example
const output = await client
 .path(
   "/documentModels/{modelId}/analyzeResults/{resultId}/figures/{figureId}",
   "prebuilt-layout",
   poller.getOperationId(),
   figureId,
 ).get().asNodeStream();

if (output.status !== "200" || !output.body) {
 throw new Error("The response was unexpected.");
}
assert.isTrue(isValidPNG(await streamToUint8Array(output.body)));
```

No additional policies
- ~A new policy `ResponseStreamToUint8ArrayPolicy` is added to the
client pipeline.~
- ~It checks if the request method is `GET` and the URL matches the
pattern `/documentModels/[^/]+/analyzeResults/[^/]+/figures/[^/]+`~
- ~If matched, streamResponseStatusCodes is set to 200, and the response
stream is converted to Uint8Array. If no readable stream is found, it
throws a RestError.~

No changes to core packages
- ~**Content-Type Check:** Introduced a check for the content type of
the response header. If the content type is `"image/png"`, the encoding
is now set to `"binary"` instead of `"utf8"`. This ensures that binary
data, like PNG images, is handled correctly without corruption.~

- ~**Encoding Parameter:** The `streamToText` function has been updated
to accept an additional parameter, `encoding`, which can be either
"utf8" or "binary". This allows for more flexible handling of different
types of response data, ensuring that the appropriate encoding is used
based on the content type.~


## TO DO
While the changes in this PR address the immediate issue with figures
API in the `@azure-rest/ai-document-intelligence` SDK, there are further
improvements needed to ensure robust handling of various content types
in the core pipeline to cater to all the packages:
- `Uint8Array` support needs to get better. Currently the `Uint8Array`
response body is corrupted.
- **Ensure codegen handles streams:** Make sure the codegen understands
which the APIs that need support for streams and handles outputs
accordingly.
- As a quicker measure, export `ResponseStreamToUint8ArrayPolicy` from
core-rest-pipeline, so that affected packages can depend on it.
- **Robust Content-Type Check:** The content-type check needs to be more
comprehensive. We need to categorize the content types for which the
responses are known to break with "utf8" encoding and apply the
appropriate encoding for each category. This will help ensure backward
compatibility and prevent regressions in the future.
- **Introduce other body properties such as `bodyAsBuffer`:** As an
alternative approach, we could introduce a new `bodyAsBuffer` or
`bodyAsUint8Array` property, etc.
2024-12-12 16:14:20 -08:00
..