Skip to content

Commit 6ed677b

Browse files
committed
Handle already read response body
1 parent c34445a commit 6ed677b

File tree

6 files changed

+23
-4
lines changed

6 files changed

+23
-4
lines changed

packages/plugins/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "seroval-plugins",
33
"type": "module",
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"files": [
66
"dist",
77
"web"

packages/plugins/tests/web/__snapshots__/response.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ exports[`Response > toCrossJSONStream > supports Response 3`] = `"{"t":34,"i":3,
2727
exports[`Response > toJSONAsync > supports Response 1`] = `"{"t":{"t":25,"i":0,"s":{"body":{"t":19,"i":1,"s":[72,101,108,108,111,32,87,111,114,108,100,33]},"options":{"t":10,"i":2,"p":{"k":["headers","status","statusText"],"v":[{"t":25,"i":3,"s":{"t":9,"i":4,"l":1,"a":[{"t":9,"i":5,"l":2,"a":[{"t":1,"s":"content-type"},{"t":1,"s":"text/plain;charset=UTF-8"}],"o":0}],"o":0},"c":"seroval-plugins/web/Headers"},{"t":0,"s":200},{"t":1,"s":""}],"s":3},"o":0}},"c":"seroval-plugins/web/Response"},"f":31,"m":[]}"`;
2828
2929
exports[`Response > toJSONAsync > supports Response 2`] = `"{"t":25,"i":0,"s":{"body":{"t":19,"i":1,"s":[72,101,108,108,111,32,87,111,114,108,100,33]},"options":{"t":10,"i":2,"p":{"k":["headers","status","statusText"],"v":[{"t":25,"i":3,"s":{"t":9,"i":4,"l":1,"a":[{"t":9,"i":5,"l":2,"a":[{"t":1,"s":"content-type"},{"t":1,"s":"text/plain;charset=UTF-8"}],"o":0}],"o":0},"c":"seroval-plugins/web/Headers"},{"t":0,"s":200},{"t":1,"s":""}],"s":3},"o":0}},"c":"seroval-plugins/web/Response"}"`;
30+
31+
exports[`Response > toJSONAsync > supports already read Response 1`] = `"{"t":{"t":25,"i":0,"s":{"body":{"t":2,"s":0},"options":{"t":10,"i":1,"p":{"k":["headers","status","statusText"],"v":[{"t":25,"i":2,"s":{"t":9,"i":3,"l":1,"a":[{"t":9,"i":4,"l":2,"a":[{"t":1,"s":"content-type"},{"t":1,"s":"text/plain;charset=UTF-8"}],"o":0}],"o":0},"c":"seroval-plugins/web/Headers"},{"t":0,"s":200},{"t":1,"s":""}],"s":3},"o":0}},"c":"seroval-plugins/web/Response"},"f":31,"m":[]}"`;

packages/plugins/tests/web/response.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,22 @@ describe('Response', () => {
4040
expect(back).toBeInstanceOf(Response);
4141
expect(await back.text()).toBe(await example.text());
4242
});
43+
44+
it('supports already read Response', async () => {
45+
const example = new Response(EXAMPLE_BODY);
46+
await example.text();
47+
const result = await toJSONAsync(example, {
48+
plugins: [ResponsePlugin],
49+
});
50+
expect(JSON.stringify(result)).toMatchSnapshot();
51+
const back = fromJSON<typeof example>(result, {
52+
plugins: [ResponsePlugin],
53+
});
54+
expect(back).toBeInstanceOf(Response);
55+
expect(back.body).toBe(null);
56+
});
4357
});
58+
4459
describe('crossSerializeAsync', () => {
4560
it('supports Response', async () => {
4661
const example = new Response(EXAMPLE_BODY);

packages/plugins/web/response.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const ResponsePlugin = /* @__PURE__ */ createPlugin<Response, ResponseNode>({
2929
async async(value, ctx) {
3030
return {
3131
body: await ctx.parse(
32-
value.body ? await value.clone().arrayBuffer() : null,
32+
value.body && !value.bodyUsed
33+
? await value.clone().arrayBuffer()
34+
: null,
3335
),
3436
options: await ctx.parse(createResponseOptions(value)),
3537
};

packages/seroval/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "seroval",
33
"type": "module",
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"files": [
66
"dist",
77
"src"

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)