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(node/http2): fixes to support grpc #20712

Merged
merged 19 commits into from
Oct 12, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
unify to use Node connect APIs
  • Loading branch information
bartlomieju committed Oct 6, 2023
commit 1dafa5b9ca7bf0e36298080da1f1fcb6659a4221
76 changes: 33 additions & 43 deletions ext/node/polyfills/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const core = globalThis.Deno.core;
import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts";
import { EventEmitter } from "node:events";
import { Buffer } from "node:buffer";
import { Server, Socket, TCP } from "node:net";
import { connect as netConnect, Server, Socket, TCP } from "node:net";
import { connect as tlsConnect } from "node:tls";
import { TypedArray } from "ext:deno_node/internal/util/types.ts";
import {
kHandle,
Expand Down Expand Up @@ -37,6 +38,7 @@ import {
ERR_HTTP2_STREAM_ERROR,
ERR_HTTP2_TRAILERS_ALREADY_SENT,
ERR_HTTP2_TRAILERS_NOT_READY,
ERR_HTTP2_UNSUPPORTED_PROTOCOL,
ERR_INVALID_HTTP_TOKEN,
ERR_SOCKET_CLOSED,
} from "ext:deno_node/internal/errors.ts";
Expand Down Expand Up @@ -355,7 +357,8 @@ export class ClientHttp2Session extends Http2Session {
#refed = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit should address #16647


constructor(
connPromise: Promise<TcpConn> | Promise<TlsConn>,
// deno-lint-ignore no-explicit-any
socket: any,
url: string,
options: Record<string, unknown>,
) {
Expand All @@ -365,16 +368,27 @@ export class ClientHttp2Session extends Http2Session {
this[kDenoConnRid] = undefined;
this[kPollConnPromiseId] = undefined;

socket.on("error", socketOnError);
socket.on("close", socketOnClose);
const connPromise = new Promise((resolve) => {
const eventName = url.startsWith("https") ? "secureConnect" : "connect";
console.log("eventName", eventName, url);
socket.once(eventName, () => {
const rid = socket[kHandle][kStreamBaseField].rid;
resolve(rid);
});
});
socket[kSession] = this;

// TODO(bartlomieju): cleanup
this.#connectPromise = (async () => {
debugHttp2(">>> before connect");
const connRid_ = await connPromise;
console.log(">>>> awaited connRid", connRid_, url);
// console.log(">>>> awaited connRid", connRid_, url);
const [clientRid, connRid] = await op_http2_connect(connRid_, url);
debugHttp2(">>> after connect", clientRid, connRid);
this[kDenoClientRid] = clientRid;
this[kDenoConnRid] = connRid;
// TODO(bartlomieju): save this promise, so the session can be unrefed
(async () => {
try {
const promise = core.opAsync(
Expand Down Expand Up @@ -1527,53 +1541,29 @@ export function connect(
host = authority.host;
}

let conn, nodeConn, url;
let url, socket;

console.log("authority", authority);
// TODO(bartlomieju): handle defaults
if (typeof options.createConnection === "function") {
// console.error("Not implemented: http2.connect.options.createConnection");
// notImplemented("http2.connect.options.createConnection");
nodeConn = options.createConnection(host, options);
url = `https://${host}${port == 80 ? "" : (":" + port)}`;
socket = options.createConnection(host, options);
} else {
// TODO(bartlomieju): using `localhost` as a `host` refuses to connect
// when server is listening on `0.0.0.0`
console.log(protocol, host, port);

if (protocol == "http:") {
conn = Deno.connect({ port, hostname: "127.0.0.1" });
url = `https://${host}${port == 80 ? "" : (":" + port)}`;
} else if (protocol == "https:") {
conn = Deno.connectTls({ port, hostname: host, alpnProtocols: ["h2"] });
url = `https://${host}${port == 443 ? "" : (":" + port)}`;
} else {
throw new TypeError("Unexpected URL protocol");
switch (protocol) {
case "http:":
url = `https://${host}${port == 80 ? "" : (":" + port)}`;
socket = netConnect({ port, host, ...options });
break;
case "https:":
// TODO(bartlomieju): handle `initializeTLSOptions` here
url = `https://${host}${port == 443 ? "" : (":" + port)}`;
socket = tlsConnect(port, host, {});
break;
default:
throw new ERR_HTTP2_UNSUPPORTED_PROTOCOL(protocol);
}
}

let connPromise;
const session = new ClientHttp2Session(socket, url, options);

if (nodeConn) {
nodeConn.on("error", socketOnError);
nodeConn.on("close", socketOnClose);
connPromise = new Promise((resolve) => {
nodeConn.once("connect", () => {
const rid = nodeConn[kHandle][kStreamBaseField].rid;
resolve(rid);
});
});
} else {
connPromise = (async () => {
const c = await conn;
return c.rid;
})();
}

const session = new ClientHttp2Session(connPromise, url, options);
if (nodeConn) {
nodeConn[kSession] = session;
}
session[kAuthority] = `${options.servername || host}:${port}`;
session[kProtocol] = protocol;

Expand Down