Unverified Commit 0413fea8 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

Turobpack: Implement Streaming App/API rendering (#47476)

Paired with https://github.com/vercel/turbo/pull/4329, this implements
streaming responses for App and API renders. This is accomplished by
sending an initial `headers` message (carrying the status code and
headers list), N `body-chunk` messages of bytes, and a final `body-end`
message to signal completion.

Once sent to Turbopack, these chunk messages will be streamed out of the
node rendering process directly into the HTTP server's response.

Closes WEB-27

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 4c60b7b6
This diff is collapsed.
......@@ -27,7 +27,9 @@ lto = true
# Workspace crates
next-build = { path = "crates/next-build" }
next-core = { path = "crates/next-core", default-features = false }
next-dev = { path = "crates/next-dev", default-features = false, features = ["serializable"] }
next-dev = { path = "crates/next-dev", default-features = false, features = [
"serializable",
] }
next-dev-tests = { path = "crates/next-dev-tests" }
next-transform-font = { path = "crates/next-transform-font" }
next-transform-dynamic = { path = "crates/next-transform-dynamic" }
......@@ -45,11 +47,11 @@ swc_emotion = { version = "0.29.10" }
testing = { version = "0.31.31" }
# Turbo crates
turbo-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230329.2" }
turbo-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230329.3" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230329.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230329.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230329.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230329.3" }
# General Deps
......@@ -67,7 +69,10 @@ chromiumoxide = { version = "0.4.0", features = [
tungstenite = "0.17.3"
# flate2_zlib requires zlib, use flate2_rust
allsorts = { version = "0.14.0", default_features = false, features = ["outline", "flate2_rust"] }
allsorts = { version = "0.14.0", default_features = false, features = [
"outline",
"flate2_rust",
] }
anyhow = "1.0.69"
assert_cmd = "2.0.8"
async-compression = { version = "0.3.13", default-features = false, features = [
......
......@@ -52,12 +52,21 @@ type IpcIncomingMessage = {
data: RenderData;
};
type IpcOutgoingMessage = {
type: "response";
statusCode: number;
headers: Array<[string, string]>;
body: string;
};
type IpcOutgoingMessage =
| {
type: "headers";
data: {
status: number;
headers: [string, string][];
};
}
| {
type: "bodyChunk";
data: number[];
}
| {
type: "bodyEnd";
};
const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
......@@ -84,10 +93,21 @@ const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
}
ipc.send({
type: "response",
statusCode: 200,
...result,
type: "headers",
data: {
status: 200,
headers: result.headers,
},
});
for await (const chunk of result.body) {
ipc.send({
type: "bodyChunk",
data: (chunk as Buffer).toJSON().data,
});
}
ipc.send({ type: "bodyEnd" });
}
})().catch((err) => {
ipc.sendError(err);
......@@ -143,10 +163,13 @@ async function runOperation(renderData: RenderData) {
};
},
};
}
};
const proxyMethodsNested = (type: "ssrModuleMapping" | "clientModules"): ProxyHandler<
ClientReferenceManifest["ssrModuleMapping"] | ClientReferenceManifest["clientModules"]
const proxyMethodsNested = (
type: "ssrModuleMapping" | "clientModules"
): ProxyHandler<
| ClientReferenceManifest["ssrModuleMapping"]
| ClientReferenceManifest["clientModules"]
> => {
return {
get(_target, key: string) {
......@@ -179,8 +202,14 @@ async function runOperation(renderData: RenderData) {
};
const proxyMethods = (): ProxyHandler<ClientReferenceManifest> => {
const clientModulesProxy = new Proxy({}, proxyMethodsNested("clientModules"));
const ssrModuleMappingProxy = new Proxy({}, proxyMethodsNested("ssrModuleMapping"));
const clientModulesProxy = new Proxy(
{},
proxyMethodsNested("clientModules")
);
const ssrModuleMappingProxy = new Proxy(
{},
proxyMethodsNested("ssrModuleMapping")
);
return {
get(_target: any, prop: string) {
if (prop === "ssrModuleMapping") {
......@@ -195,7 +224,10 @@ async function runOperation(renderData: RenderData) {
},
};
};
const manifest: ClientReferenceManifest = new Proxy({} as any, proxyMethods());
const manifest: ClientReferenceManifest = new Proxy(
{} as any,
proxyMethods()
);
const serverCSSManifest: ClientCSSReferenceManifest = {
cssImports: {},
cssModules: {},
......@@ -265,18 +297,11 @@ async function runOperation(renderData: RenderData) {
if (!result || result.isNull())
throw new Error("rendering was not successful");
let body;
const body = new PassThrough();
if (result.isDynamic()) {
const stream = new PassThrough();
result.pipe(stream);
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
body = Buffer.concat(chunks).toString();
result.pipe(body);
} else {
body = result.toUnchunkedString();
body.write(result.toUnchunkedString());
}
return {
headers: [
......
......@@ -19,7 +19,7 @@ type IpcIncomingMessage =
}
| {
type: "bodyChunk";
data: Array<number>;
data: number[];
}
| { type: "bodyEnd" };
......@@ -29,8 +29,11 @@ type IpcOutgoingMessage =
data: ResponseHeaders;
}
| {
type: "body";
data: Array<number>;
type: "bodyChunk";
data: number[];
}
| {
type: "bodyEnd";
};
type ResponseHeaders = {
......@@ -137,7 +140,6 @@ export default function startHandler(handler: Handler): void {
server: Server,
clientResponse: IncomingMessage
) {
const responseData: Buffer[] = [];
const responseHeaders: ResponseHeaders = {
status: clientResponse.statusCode!,
headers: toPairs(clientResponse.rawHeaders),
......@@ -149,14 +151,14 @@ export default function startHandler(handler: Handler): void {
});
clientResponse.on("data", (chunk) => {
responseData.push(chunk);
ipc.send({
type: "bodyChunk",
data: chunk.toJSON().data,
});
});
clientResponse.once("end", () => {
ipc.send({
type: "body",
data: Buffer.concat(responseData).toJSON().data,
});
ipc.send({ type: "bodyEnd" });
server.close();
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment