Skip to content

Commit

Permalink
Use NEXT_DEPLOYMENT_ID instead of NEXT_RUNTIME
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Oct 24, 2024
1 parent 6d26f38 commit 03b472f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
19 changes: 2 additions & 17 deletions library/sources/HTTPServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,21 @@ import { Hooks } from "../agent/hooks/Hooks";
import { wrapExport } from "../agent/hooks/wrapExport";
import { wrapNewInstance } from "../agent/hooks/wrapNewInstance";
import { Wrapper } from "../agent/Wrapper";
import { isPackageInstalled } from "../helpers/isPackageInstalled";
import { createRequestListener } from "./http-server/createRequestListener";
import { createStreamListener } from "./http-server/http2/createStreamListener";

export class HTTPServer implements Wrapper {
private isNextJS() {
return process.env.NEXT_RUNTIME && process.env.NEXT_RUNTIME.length > 0;
}

private wrapRequestListener(args: unknown[], module: string, agent: Agent) {
// Parse body only if next is installed
// We can only read the body stream once
// This is tricky, see replaceRequestBody(...)
// e.g. Hono uses web requests and web streams
// (uses Readable.toWeb(req) to convert to a web stream)
const parseBody = this.isNextJS() || isPackageInstalled("micro");

// Without options
// http(s).createServer(listener)
if (args.length > 0 && typeof args[0] === "function") {
return [createRequestListener(args[0], module, agent, parseBody)];
return [createRequestListener(args[0], module, agent)];
}

// With options
// http(s).createServer({ ... }, listener)
if (args.length > 1 && typeof args[1] === "function") {
return [
args[0],
createRequestListener(args[1], module, agent, parseBody),
];
return [args[0], createRequestListener(args[1], module, agent)];
}

return args;
Expand Down
13 changes: 11 additions & 2 deletions library/sources/http-server/createRequestListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IncomingMessage, RequestListener, ServerResponse } from "http";
import { Agent } from "../../agent/Agent";
import { bindContext, getContext, runWithContext } from "../../agent/Context";
import { escapeHTML } from "../../helpers/escapeHTML";
import { isPackageInstalled } from "../../helpers/isPackageInstalled";
import { contextFromRequest } from "./contextFromRequest";
import { ipAllowedToAccessRoute } from "./ipAllowedToAccessRoute";
import { readBodyStream } from "./readBodyStream";
Expand All @@ -10,10 +11,18 @@ import { shouldDiscoverRoute } from "./shouldDiscoverRoute";
export function createRequestListener(
listener: Function,
module: string,
agent: Agent,
readBody: boolean
agent: Agent
): RequestListener {
const isMicroInstalled = isPackageInstalled("micro");

return async function requestListener(req, res) {
// Parse body only if next or micro is installed
// We can only read the body stream once
// This is tricky, see replaceRequestBody(...)
// e.g. Hono uses web requests and web streams
// (uses Readable.toWeb(req) to convert to a web stream)
const readBody = "NEXT_DEPLOYMENT_ID" in process.env || isMicroInstalled;

if (!readBody) {
return callListenerWithContext(listener, req, res, module, agent, "");
}
Expand Down

0 comments on commit 03b472f

Please sign in to comment.