Skip to content

Commit

Permalink
[core-client] allow serializing browser ReadableStream (#27052)
Browse files Browse the repository at this point in the history
We have browser stream support in fetchHttpClient but miss to allow it
to be serialized so an error is thrown when serializing a browser
ReadableStream.

This PR adds the condition for browser ReadableStream when checking
whether the type is supported.


### Packages impacted by this PR
`@azure/core-client`
  • Loading branch information
jeremymeng authored Sep 8, 2023
1 parent b8c6c25 commit a916d2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sdk/core/core-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bugs Fixed

- Fix an error when serializing browser ReadableStream [PR #27052](https://github.com/Azure/azure-sdk-for-js/pull/27052)

### Other Changes

## 1.7.3 (2023-06-01)
Expand Down
5 changes: 3 additions & 2 deletions sdk/core/core-client/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,16 @@ function serializeBasicTypes(typeName: string, objectName: string, value: any):
const objectType = typeof value;
if (
objectType !== "string" &&
typeof value.pipe !== "function" &&
typeof value.pipe !== "function" && // NodeJS.ReadableStream
typeof value.tee !== "function" && // browser ReadableStream
!(value instanceof ArrayBuffer) &&
!ArrayBuffer.isView(value) &&
// File objects count as a type of Blob, so we want to use instanceof explicitly
!((typeof Blob === "function" || typeof Blob === "object") && value instanceof Blob) &&
objectType !== "function"
) {
throw new Error(
`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, NodeJS.ReadableStream, or () => NodeJS.ReadableStream.`
`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, ReadableStream, or () => ReadableStream.`
);
}
}
Expand Down
15 changes: 15 additions & 0 deletions sdk/core/core-client/test/browser/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,20 @@ describe("Serializer (browser specific)", function () {
const result = serializer.serialize(mapper, file);
assert.strictEqual(result, file, "Expect file streams to be left intact");
});

it("Should accept ReadableStream", function () {
const stream = new ReadableStream();

const serializer = createSerializer();

const mapper: Mapper = {
type: { name: "Stream" },
required: true,
serializedName: "Stream",
};

const result = serializer.serialize(mapper, stream);
assert.strictEqual(result, stream, "Expect stream to be left intact");
});
});
});

0 comments on commit a916d2f

Please sign in to comment.