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 ...@@ -27,7 +27,9 @@ lto = true
# Workspace crates # Workspace crates
next-build = { path = "crates/next-build" } next-build = { path = "crates/next-build" }
next-core = { path = "crates/next-core", default-features = false } 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-dev-tests = { path = "crates/next-dev-tests" }
next-transform-font = { path = "crates/next-transform-font" } next-transform-font = { path = "crates/next-transform-font" }
next-transform-dynamic = { path = "crates/next-transform-dynamic" } next-transform-dynamic = { path = "crates/next-transform-dynamic" }
...@@ -45,11 +47,11 @@ swc_emotion = { version = "0.29.10" } ...@@ -45,11 +47,11 @@ swc_emotion = { version = "0.29.10" }
testing = { version = "0.31.31" } testing = { version = "0.31.31" }
# Turbo crates # 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.. # [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 # [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 # General Deps
...@@ -67,7 +69,10 @@ chromiumoxide = { version = "0.4.0", features = [ ...@@ -67,7 +69,10 @@ chromiumoxide = { version = "0.4.0", features = [
tungstenite = "0.17.3" tungstenite = "0.17.3"
# flate2_zlib requires zlib, use flate2_rust # 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" anyhow = "1.0.69"
assert_cmd = "2.0.8" assert_cmd = "2.0.8"
async-compression = { version = "0.3.13", default-features = false, features = [ async-compression = { version = "0.3.13", default-features = false, features = [
......
...@@ -52,12 +52,21 @@ type IpcIncomingMessage = { ...@@ -52,12 +52,21 @@ type IpcIncomingMessage = {
data: RenderData; data: RenderData;
}; };
type IpcOutgoingMessage = { type IpcOutgoingMessage =
type: "response"; | {
statusCode: number; type: "headers";
headers: Array<[string, string]>; data: {
body: string; status: number;
}; headers: [string, string][];
};
}
| {
type: "bodyChunk";
data: number[];
}
| {
type: "bodyEnd";
};
const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8"; const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
...@@ -84,11 +93,22 @@ const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8"; ...@@ -84,11 +93,22 @@ const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
} }
ipc.send({ ipc.send({
type: "response", type: "headers",
statusCode: 200, data: {
...result, 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) => { })().catch((err) => {
ipc.sendError(err); ipc.sendError(err);
}); });
...@@ -143,10 +163,13 @@ async function runOperation(renderData: RenderData) { ...@@ -143,10 +163,13 @@ async function runOperation(renderData: RenderData) {
}; };
}, },
}; };
} };
const proxyMethodsNested = (type: "ssrModuleMapping" | "clientModules"): ProxyHandler< const proxyMethodsNested = (
ClientReferenceManifest["ssrModuleMapping"] | ClientReferenceManifest["clientModules"] type: "ssrModuleMapping" | "clientModules"
): ProxyHandler<
| ClientReferenceManifest["ssrModuleMapping"]
| ClientReferenceManifest["clientModules"]
> => { > => {
return { return {
get(_target, key: string) { get(_target, key: string) {
...@@ -179,8 +202,14 @@ async function runOperation(renderData: RenderData) { ...@@ -179,8 +202,14 @@ async function runOperation(renderData: RenderData) {
}; };
const proxyMethods = (): ProxyHandler<ClientReferenceManifest> => { const proxyMethods = (): ProxyHandler<ClientReferenceManifest> => {
const clientModulesProxy = new Proxy({}, proxyMethodsNested("clientModules")); const clientModulesProxy = new Proxy(
const ssrModuleMappingProxy = new Proxy({}, proxyMethodsNested("ssrModuleMapping")); {},
proxyMethodsNested("clientModules")
);
const ssrModuleMappingProxy = new Proxy(
{},
proxyMethodsNested("ssrModuleMapping")
);
return { return {
get(_target: any, prop: string) { get(_target: any, prop: string) {
if (prop === "ssrModuleMapping") { if (prop === "ssrModuleMapping") {
...@@ -195,7 +224,10 @@ async function runOperation(renderData: RenderData) { ...@@ -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 = { const serverCSSManifest: ClientCSSReferenceManifest = {
cssImports: {}, cssImports: {},
cssModules: {}, cssModules: {},
...@@ -265,18 +297,11 @@ async function runOperation(renderData: RenderData) { ...@@ -265,18 +297,11 @@ async function runOperation(renderData: RenderData) {
if (!result || result.isNull()) if (!result || result.isNull())
throw new Error("rendering was not successful"); throw new Error("rendering was not successful");
let body; const body = new PassThrough();
if (result.isDynamic()) { if (result.isDynamic()) {
const stream = new PassThrough(); result.pipe(body);
result.pipe(stream);
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
body = Buffer.concat(chunks).toString();
} else { } else {
body = result.toUnchunkedString(); body.write(result.toUnchunkedString());
} }
return { return {
headers: [ headers: [
......
...@@ -19,7 +19,7 @@ type IpcIncomingMessage = ...@@ -19,7 +19,7 @@ type IpcIncomingMessage =
} }
| { | {
type: "bodyChunk"; type: "bodyChunk";
data: Array<number>; data: number[];
} }
| { type: "bodyEnd" }; | { type: "bodyEnd" };
...@@ -29,8 +29,11 @@ type IpcOutgoingMessage = ...@@ -29,8 +29,11 @@ type IpcOutgoingMessage =
data: ResponseHeaders; data: ResponseHeaders;
} }
| { | {
type: "body"; type: "bodyChunk";
data: Array<number>; data: number[];
}
| {
type: "bodyEnd";
}; };
type ResponseHeaders = { type ResponseHeaders = {
...@@ -137,7 +140,6 @@ export default function startHandler(handler: Handler): void { ...@@ -137,7 +140,6 @@ export default function startHandler(handler: Handler): void {
server: Server, server: Server,
clientResponse: IncomingMessage clientResponse: IncomingMessage
) { ) {
const responseData: Buffer[] = [];
const responseHeaders: ResponseHeaders = { const responseHeaders: ResponseHeaders = {
status: clientResponse.statusCode!, status: clientResponse.statusCode!,
headers: toPairs(clientResponse.rawHeaders), headers: toPairs(clientResponse.rawHeaders),
...@@ -149,14 +151,14 @@ export default function startHandler(handler: Handler): void { ...@@ -149,14 +151,14 @@ export default function startHandler(handler: Handler): void {
}); });
clientResponse.on("data", (chunk) => { clientResponse.on("data", (chunk) => {
responseData.push(chunk); ipc.send({
type: "bodyChunk",
data: chunk.toJSON().data,
});
}); });
clientResponse.once("end", () => { clientResponse.once("end", () => {
ipc.send({ ipc.send({ type: "bodyEnd" });
type: "body",
data: Buffer.concat(responseData).toJSON().data,
});
server.close(); 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