Skip to content

Update proxyResponse method #6005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Run lifecycle hooks for specific codebases. (#6011)
- Fixed issue causing `firebase emulators:start` to crash in Next.js apps (#6005)
4 changes: 2 additions & 2 deletions firebase-vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 93 additions & 17 deletions src/frameworks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,97 @@ export async function warnIfCustomBuildScript(
}
}

function proxyResponse(original: ServerResponse, next: () => void) {
return (response: IncomingMessage | ServerResponse) => {
const { statusCode, statusMessage } = response;
if (!statusCode) {
original.end();
return;
}
if (statusCode === 404) {
return next();
}
const headers = "getHeaders" in response ? response.getHeaders() : response.headers;
original.writeHead(statusCode, statusMessage, headers);
response.pipe(original);
};
/**
* Proxy a HTTP response
* It uses the Proxy object to intercept the response and buffer it until the
* response is finished. This allows us to modify the response before sending
* it back to the client.
*/
export function proxyResponse(
req: IncomingMessage,
res: ServerResponse,
next: () => void
): ServerResponse {
const proxiedRes = new ServerResponse(req);
// Object to store the original response methods
const buffer: [
string,
Parameters<ServerResponse["write" | "setHeader" | "removeHeader" | "writeHead" | "end"]>
][] = [];

// Proxy the response methods
// The apply handler is called when the method e.g. write, setHeader, etc. is called
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/apply
// The target is the original method
// The thisArg is the proxied response
// The args are the arguments passed to the method
proxiedRes.write = new Proxy(proxiedRes.write.bind(proxiedRes), {
apply: (
target: ServerResponse["write"],
thisArg: ServerResponse,
args: Parameters<ServerResponse["write"]>
) => {
// call the original write method on the proxied response
target.call(thisArg, ...args);
// store the method call in the buffer
buffer.push(["write", args]);
},
});

proxiedRes.setHeader = new Proxy(proxiedRes.setHeader.bind(proxiedRes), {
apply: (
target: ServerResponse["setHeader"],
thisArg: ServerResponse,
args: Parameters<ServerResponse["setHeader"]>
) => {
target.call(thisArg, ...args);
buffer.push(["setHeader", args]);
},
});
proxiedRes.removeHeader = new Proxy(proxiedRes.removeHeader.bind(proxiedRes), {
apply: (
target: ServerResponse["removeHeader"],
thisArg: ServerResponse,
args: Parameters<ServerResponse["removeHeader"]>
) => {
target.call(thisArg, ...args);
buffer.push(["removeHeader", args]);
},
});
proxiedRes.writeHead = new Proxy(proxiedRes.writeHead.bind(proxiedRes), {
apply: (
target: ServerResponse["writeHead"],
thisArg: ServerResponse,
args: Parameters<ServerResponse["writeHead"]>
) => {
target.call(thisArg, ...args);
buffer.push(["writeHead", args]);
},
});
proxiedRes.end = new Proxy(proxiedRes.end.bind(proxiedRes), {
apply: (
target: ServerResponse["end"],
thisArg: ServerResponse,
args: Parameters<ServerResponse["end"]>
) => {
// call the original end method on the proxied response
target.call(thisArg, ...args);
// if the proxied response is a 404, call next to continue down the middleware chain
// otherwise, send the buffered response i.e. call the original response methods: write, setHeader, etc.
// and then end the response and clear the buffer
if (proxiedRes.statusCode === 404) {
next();
} else {
for (const [fn, args] of buffer) {
(res as any)[fn](...args);
}
res.end(...args);
buffer.length = 0;
}
},
});

return proxiedRes;
}

export function simpleProxy(hostOrRequestHandler: string | RequestHandler) {
Expand Down Expand Up @@ -127,9 +204,8 @@ export function simpleProxy(hostOrRequestHandler: string | RequestHandler) {
originalRes.end();
});
} else {
await Promise.resolve(hostOrRequestHandler(originalReq, originalRes, next));
const proxiedRes = new ServerResponse(originalReq);
proxyResponse(originalRes, next)(proxiedRes);
const proxiedRes = proxyResponse(originalReq, originalRes, next);
await hostOrRequestHandler(originalReq, proxiedRes, next);
}
};
}
Expand Down