Skip to content

Commit a916d2f

Browse files
authored
[core-client] allow serializing browser ReadableStream (#27052)
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`
1 parent b8c6c25 commit a916d2f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

sdk/core/core-client/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fix an error when serializing browser ReadableStream [PR #27052](https://github.com/Azure/azure-sdk-for-js/pull/27052)
12+
1113
### Other Changes
1214

1315
## 1.7.3 (2023-06-01)

sdk/core/core-client/src/serializer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,16 @@ function serializeBasicTypes(typeName: string, objectName: string, value: any):
430430
const objectType = typeof value;
431431
if (
432432
objectType !== "string" &&
433-
typeof value.pipe !== "function" &&
433+
typeof value.pipe !== "function" && // NodeJS.ReadableStream
434+
typeof value.tee !== "function" && // browser ReadableStream
434435
!(value instanceof ArrayBuffer) &&
435436
!ArrayBuffer.isView(value) &&
436437
// File objects count as a type of Blob, so we want to use instanceof explicitly
437438
!((typeof Blob === "function" || typeof Blob === "object") && value instanceof Blob) &&
438439
objectType !== "function"
439440
) {
440441
throw new Error(
441-
`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, NodeJS.ReadableStream, or () => NodeJS.ReadableStream.`
442+
`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, ReadableStream, or () => ReadableStream.`
442443
);
443444
}
444445
}

sdk/core/core-client/test/browser/serializer.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,20 @@ describe("Serializer (browser specific)", function () {
2424
const result = serializer.serialize(mapper, file);
2525
assert.strictEqual(result, file, "Expect file streams to be left intact");
2626
});
27+
28+
it("Should accept ReadableStream", function () {
29+
const stream = new ReadableStream();
30+
31+
const serializer = createSerializer();
32+
33+
const mapper: Mapper = {
34+
type: { name: "Stream" },
35+
required: true,
36+
serializedName: "Stream",
37+
};
38+
39+
const result = serializer.serialize(mapper, stream);
40+
assert.strictEqual(result, stream, "Expect stream to be left intact");
41+
});
2742
});
2843
});

0 commit comments

Comments
 (0)