Skip to content

Commit 26fa47d

Browse files
committed
Handle already read request body
1 parent 6ed677b commit 26fa47d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ exports[`Request > crossSerializeStream > supports Request 3`] = `"$R[4].return(
1818
1919
exports[`Request > serializeAsync > supports Request 1`] = `"new Request("http://localhost:3000/",{body:new Uint8Array([72,101,108,108,111,32,87,111,114,108,100,33]).buffer,cache:"default",credentials:"same-origin",headers:new Headers([["content-type","text/plain;charset=UTF-8"]]),integrity:"",keepalive:!1,method:"POST",mode:"cors",redirect:"follow",referrer:"about:client",referrerPolicy:""})"`;
2020
21+
exports[`Request > serializeAsync > supports already read Request 1`] = `"new Request("http://localhost:3000/",{body:null,cache:"default",credentials:"same-origin",headers:new Headers([["content-type","text/plain;charset=UTF-8"]]),integrity:"",keepalive:!1,method:"POST",mode:"cors",redirect:"follow",referrer:"about:client",referrerPolicy:""})"`;
22+
2123
exports[`Request > toCrossJSONStream > supports Request 1`] = `"{"t":25,"i":0,"s":{"url":{"t":1,"s":"http://localhost:3000/"},"options":{"t":10,"i":1,"p":{"k":["body","cache","credentials","headers","integrity","keepalive","method","mode","redirect","referrer","referrerPolicy"],"v":[{"t":25,"i":2,"s":{"factory":{"t":25,"i":3,"c":"seroval-plugins/web/ReadableStreamFactory"},"stream":{"t":31,"i":4,"a":[],"f":{"t":26,"i":5,"s":4}}},"c":"seroval/plugins/web/ReadableStream"},{"t":1,"s":"default"},{"t":1,"s":"same-origin"},{"t":25,"i":6,"s":{"t":9,"i":7,"l":1,"a":[{"t":9,"i":8,"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":1,"s":""},{"t":2,"s":3},{"t":1,"s":"POST"},{"t":1,"s":"cors"},{"t":1,"s":"follow"},{"t":1,"s":"about:client"},{"t":1,"s":""}],"s":11},"o":0}},"c":"seroval-plugins/web/Request"}"`;
2224
2325
exports[`Request > toCrossJSONStream > supports Request 2`] = `"{"t":32,"i":4,"f":{"t":15,"i":9,"l":12,"c":"Uint8Array","f":{"t":19,"i":10,"s":[72,101,108,108,111,32,87,111,114,108,100,33]},"b":0}}"`;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ describe('Request', () => {
3232
expect(back.url).toBe(example.url);
3333
expect(back.method).toBe(example.method);
3434
});
35+
36+
it('supports already read Request', async () => {
37+
const example = new Request(EXAMPLE_URL, {
38+
method: 'POST',
39+
body: EXAMPLE_BODY,
40+
});
41+
await example.text();
42+
const result = await serializeAsync(example, {
43+
plugins: [RequestPlugin],
44+
});
45+
expect(result).toMatchSnapshot();
46+
const back = deserialize<typeof example>(result);
47+
expect(back).toBeInstanceOf(Request);
48+
expect(back.body).toBe(null);
49+
expect(back.url).toBe(example.url);
50+
expect(back.method).toBe(example.method);
51+
});
3552
});
3653
describe('toJSONAsync', () => {
3754
it('supports Request', async () => {

packages/plugins/web/request.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ const RequestPlugin = /* @__PURE__ */ createPlugin<Request, RequestNode>({
4343
options: await ctx.parse(
4444
createRequestOptions(
4545
value,
46-
value.body ? await value.clone().arrayBuffer() : null,
46+
value.body && !value.bodyUsed
47+
? await value.clone().arrayBuffer()
48+
: null,
4749
),
4850
),
4951
};

0 commit comments

Comments
 (0)