Skip to content
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

fix: dd-trace http message compat #25021

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
56 changes: 34 additions & 22 deletions ext/node/polyfills/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from "ext:deno_node/internal/validators.mjs";
import {
addAbortSignal,
Duplex as NodeDuplex,
finished,
Readable as NodeReadable,
Writable as NodeWritable,
Expand Down Expand Up @@ -290,12 +291,14 @@ class FakeSocket extends EventEmitter {
encrypted?: boolean | undefined;
remotePort?: number | undefined;
remoteAddress?: string | undefined;
reader?: ReadableStreamDefaultReader | undefined;
} = {},
) {
super();
this.remoteAddress = opts.remoteAddress;
this.remotePort = opts.remotePort;
this.encrypted = opts.encrypted;
this.reader = opts.reader;
this.writable = true;
this.readable = true;
}
Expand Down Expand Up @@ -1613,17 +1616,17 @@ export class ServerResponse extends NodeWritable {

// TODO(@AaronO): optimize
export class IncomingMessageForServer extends NodeReadable {
#req: Request;
#headers: Record<string, string>;
url: string;
method: string;
// Polyfills part of net.Socket object.
// These properties are used by `npm:forwarded` for example.
socket: { remoteAddress: string; remotePort: number };

constructor(req: Request, socket: FakeSocket) {
// Check if no body (GET/HEAD/OPTIONS/...)
const reader = req.body?.getReader();
socket: Socket | FakeSocket;

constructor(socket: FakeSocket | Socket) {
const reader = socket instanceof FakeSocket
? socket.reader
: socket instanceof Socket
? NodeDuplex.toWeb(socket).readable.getReader()
: null;
super({
autoDestroy: true,
emitClose: true,
Expand All @@ -1646,12 +1649,11 @@ export class IncomingMessageForServer extends NodeReadable {
}).finally(nextTick(onError, this, err, cb));
},
});
// TODO(@bartlomieju): consider more robust path extraction, e.g:
// url: (new URL(request.url).pathname),
this.url = req.url?.slice(req.url.indexOf("/", 8));
this.method = req.method;
this.url = "";
this.method = "";
this.socket = socket;
this.#req = req;
this.upgrade = null;
this.rawHeaders = [];
}

get aborted() {
Expand All @@ -1669,7 +1671,7 @@ export class IncomingMessageForServer extends NodeReadable {
get headers() {
if (!this.#headers) {
this.#headers = {};
const entries = headersEntries(this.#req.headers);
const entries = headersEntries(this.rawHeaders);
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
this.#headers[entry[0]] = entry[1];
Expand All @@ -1682,17 +1684,18 @@ export class IncomingMessageForServer extends NodeReadable {
this.#headers = val;
}

get upgrade(): boolean {
return Boolean(
this.#req.headers.get("connection")?.toLowerCase().includes("upgrade") &&
this.#req.headers.get("upgrade"),
);
}

// connection is deprecated, but still tested in unit test.
get connection() {
return this.socket;
}

setTimeout(msecs, callback) {
if (callback) {
this.on("timeout", callback);
}
this.socket.setTimeout(msecs);
return this;
}
}

export type ServerHandler = (
Expand Down Expand Up @@ -1774,8 +1777,17 @@ export class ServerImpl extends EventEmitter {
remoteAddress: info.remoteAddr.hostname,
remotePort: info.remoteAddr.port,
encrypted: this._encrypted,
reader: request.body?.getReader(),
});
const req = new IncomingMessageForServer(request, socket);

const req = new IncomingMessageForServer(socket);
req.url = request.url?.slice(req.url.indexOf("/", 8));
req.method = request.method;
req.upgrade =
request.headers.get("connection")?.toLowerCase().includes("upgrade") &&
request.headers.get("upgrade");
req.rawHeaders = request.headers;

if (req.upgrade && this.listenerCount("upgrade") > 0) {
const { conn, response } = upgradeHttpRaw(request);
const socket = new Socket({
Expand Down