ensure the client correctly handles all binary data (#8322)

* fix: walk blobs ignoring blobs in arrays directly

* add changeset

* fix function

* add changeset

* address review comments

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: pngwn <hello@pngwn.io>
This commit is contained in:
Liam Dyer 2024-05-21 16:11:35 -04:00 committed by GitHub
parent 82ba397592
commit 47012a0c4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 32 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---
fix:ensure the client correctly handles all binary data

View File

@ -41,13 +41,15 @@ export async function walk_and_store_blobs(
let blob_refs: BlobRef[] = [];
await Promise.all(
data.map(async (item) => {
data.map(async (_, index) => {
let new_path = path.slice();
new_path.push(item);
new_path.push(String(index));
const array_refs = await walk_and_store_blobs(
data[item],
root ? endpoint_info?.parameters[item]?.component || undefined : type,
data[index],
root
? endpoint_info?.parameters[index]?.component || undefined
: type,
new_path,
false,
endpoint_info
@ -87,22 +89,6 @@ export async function walk_and_store_blobs(
);
}
if (
!blob_refs.length &&
!(
data instanceof Blob ||
data instanceof ArrayBuffer ||
data instanceof Uint8Array
)
) {
return [
{
path: path,
blob: new NodeBlob([JSON.stringify(data)]),
type: typeof data
}
];
}
return blob_refs;
}

View File

@ -43,6 +43,15 @@ describe("walk_and_store_blobs", () => {
expect(parts[0].blob).toBe(false);
});
it("should handle arrays", async () => {
const image = new Blob([]);
const parts = await walk_and_store_blobs([image]);
expect(parts).toHaveLength(1);
expect(parts[0].blob).toBeInstanceOf(NodeBlob);
expect(parts[0].path).toEqual(["0"]);
});
it("should handle deep structures", async () => {
const image = new Blob([]);
const parts = await walk_and_store_blobs({ a: { b: { data: { image } } } });
@ -142,18 +151,6 @@ describe("walk_and_store_blobs", () => {
expect(parts[0].path).toEqual(["a", "b", "data", "image"]);
expect(parts[0].blob).toBeInstanceOf(NodeBlob);
});
it("should convert an object with primitive values to BlobRefs", async () => {
const param = {
test: "test"
};
const parts = await walk_and_store_blobs(param);
expect(parts).toHaveLength(1);
expect(parts[0].path).toEqual([]);
expect(parts[0].blob).toBeInstanceOf(NodeBlob);
expect(parts[0].type).toEqual("object");
});
});
describe("update_object", () => {
it("should update the value of a nested property", () => {