Skip to content

Commit

Permalink
feat(proxy): stream request body with streamRequest option (#374)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
iainsproat and pi0 authored Jul 25, 2023
1 parent 78aec63 commit 8c73933
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type { H3Event } from "../event";
import type { H3EventContext, RequestHeaders } from "../types";
import { getMethod, getRequestHeaders } from "./request";
import { readRawBody } from "./body";
import { splitCookiesString } from "./cookie";
import { sanitizeStatusMessage, sanitizeStatusCode } from "./sanitize";
import { readRawBody } from "./body";

export type Duplex = "half" | "full";

export interface ProxyOptions {
headers?: RequestHeaders | HeadersInit;
fetchOptions?: RequestInit;
fetchOptions?: RequestInit & { duplex?: Duplex };
fetch?: typeof fetch;
sendStream?: boolean;
streamRequest?: boolean;
cookieDomainRewrite?: string | Record<string, string>;
cookiePathRewrite?: string | Record<string, string>;
onResponse?: (event: H3Event, response: Response) => void;
Expand All @@ -35,8 +38,14 @@ export async function proxyRequest(

// Body
let body;
let duplex: Duplex | undefined;
if (PayloadMethods.has(method)) {
body = await readRawBody(event, false).catch(() => undefined);
if (opts.streamRequest) {
body = event.body;
duplex = "half";
} else {
body = await readRawBody(event, false).catch(() => undefined);
}
}

// Headers
Expand All @@ -54,6 +63,7 @@ export async function proxyRequest(
headers,
method,
body,
duplex,
...opts.fetchOptions,
},
});
Expand Down
54 changes: 54 additions & 0 deletions test/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,60 @@ describe("", () => {
expect(resBody.headers["x-req-header"]).toEqual("works");
expect(resBody.bytes).toEqual(dummyFile.length);
});

it("can proxy stream request", async () => {
app.use(
"/debug",
eventHandler(async (event) => {
return {
body: await readRawBody(event),
headers: getHeaders(event),
};
})
);

app.use(
"/",
eventHandler((event) => {
return proxyRequest(event, url + "/debug", { fetch });
})
);

const isNode16 = process.version.startsWith("v16.");
const body = isNode16
? "This is a streamed request."
: new ReadableStream({
start(controller) {
controller.enqueue("This ");
controller.enqueue("is ");
controller.enqueue("a ");
controller.enqueue("streamed ");
controller.enqueue("request.");
controller.close();
},
}).pipeThrough(new TextEncoderStream());

const res = await fetch(url + "/", {
method: "POST",
// @ts-ignore
duplex: "half",
body,
headers: {
"content-type": "application/octet-stream",
"x-custom": "hello",
"content-length": "27",
},
});
const resBody = await res.json();

expect(resBody.headers["content-type"]).toEqual(
"application/octet-stream"
);
expect(resBody.headers["x-custom"]).toEqual("hello");
expect(resBody.body).toMatchInlineSnapshot(
'"This is a streamed request."'
);
});
});

describe("multipleCookies", () => {
Expand Down

0 comments on commit 8c73933

Please sign in to comment.