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

feat(unstable): WebTransport #27431

Merged
merged 1 commit into from
Jan 29, 2025
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
feat(unstable): WebTransport
  • Loading branch information
devsnek committed Jan 29, 2025
commit dce204af32e4b56681be4f9a034256d979a3ce4b
15 changes: 15 additions & 0 deletions Cargo.lock

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

58 changes: 56 additions & 2 deletions ext/net/03_quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
op_quic_send_stream_get_id,
op_quic_send_stream_get_priority,
op_quic_send_stream_set_priority,
op_webtransport_accept,
op_webtransport_connect,
} from "ext:core/ops";
import {
getReadableStreamResourceBacking,
Expand All @@ -50,6 +52,7 @@ const {
const {
ObjectPrototypeIsPrototypeOf,
PromisePrototypeThen,
ReflectConstruct,
Symbol,
SymbolAsyncIterator,
SafePromisePrototypeFinally,
Expand Down Expand Up @@ -205,6 +208,9 @@ class QuicIncoming {
}
}

let webtransportConnect;
let webtransportAccept;

class QuicConn {
#resource;
#bidiStream = null;
Expand Down Expand Up @@ -309,6 +315,43 @@ class QuicConn {
close({ closeCode = 0, reason = "" } = { __proto__: null }) {
op_quic_connection_close(this.#resource, closeCode, reason);
}

static {
webtransportConnect = async function webtransportConnect(conn, url) {
const {
0: connectTxRid,
1: connectRxRid,
Comment on lines +319 to +323
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if I fully grasp this - why are these two functions created in a static block of a class?

Copy link
Member Author

Choose a reason for hiding this comment

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

they access a private field

2: settingsTxRid,
3: settingsRxRid,
} = await op_webtransport_connect(conn.#resource, url);
const connect = new QuicBidirectionalStream(
connectTxRid,
connectRxRid,
conn.closed,
);
const settingsTx = writableStream(settingsTxRid, conn.closed);
const settingsRx = readableStream(settingsRxRid, conn.closed);
return { connect, settingsTx, settingsRx };
};

webtransportAccept = async function webtransportAccept(conn) {
const {
0: url,
1: connectTxRid,
2: connectRxRid,
3: settingsTxRid,
4: settingsRxRid,
} = await op_webtransport_accept(conn.#resource);
const connect = new QuicBidirectionalStream(
connectTxRid,
connectRxRid,
conn.closed,
);
const settingsTx = writableStream(settingsTxRid, conn.closed);
const settingsRx = readableStream(settingsRxRid, conn.closed);
return { url, connect, settingsTx, settingsRx };
};
}
}

class QuicSendStream extends WritableStream {
Expand Down Expand Up @@ -345,15 +388,23 @@ function readableStream(rid, closed) {
SafePromisePrototypeFinally(closed, () => {
core.tryClose(rid);
});
return readableStreamForRid(rid, true, QuicReceiveStream);
return readableStreamForRid(
rid,
true,
(...args) => ReflectConstruct(QuicReceiveStream, args),
);
}

function writableStream(rid, closed) {
// stream can be indirectly closed by closing connection.
SafePromisePrototypeFinally(closed, () => {
core.tryClose(rid);
});
return writableStreamForRid(rid, true, QuicSendStream);
return writableStreamForRid(
rid,
true,
(...args) => ReflectConstruct(QuicSendStream, args),
);
}

class QuicBidirectionalStream {
Expand Down Expand Up @@ -421,6 +472,7 @@ function connectQuic(options) {
caCerts: options.caCerts,
alpnProtocols: options.alpnProtocols,
serverName: options.serverName,
serverCertificateHashes: options.serverCertificateHashes,
},
transportOptions(options),
keyPair,
Expand Down Expand Up @@ -448,4 +500,6 @@ export {
QuicListener,
QuicReceiveStream,
QuicSendStream,
webtransportAccept,
webtransportConnect,
};
3 changes: 3 additions & 0 deletions ext/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pin-project.workspace = true
quinn = { version = "0.11.6", default-features = false, features = ["runtime-tokio", "rustls", "ring"] }
rustls-tokio-stream.workspace = true
serde.workspace = true
sha2.workspace = true
socket2.workspace = true
thiserror.workspace = true
tokio.workspace = true
url.workspace = true
web-transport-proto = "0.2.3"
12 changes: 12 additions & 0 deletions ext/net/lib.deno_net.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,5 +851,17 @@ declare namespace Deno {
options: ConnectQuicOptions<ZRTT>,
): ZRTT extends true ? (QuicConn | Promise<QuicConn>) : Promise<QuicConn>;

/**
* **UNSTABLE**: New API, yet to be vetted.
*
* Upgrade a QUIC connection into a WebTransport instance.
*
* @category Network
* @experimental
*/
export function upgradeWebTransport(
conn: QuicConn,
): Promise<WebTransport & { url: string }>;

export {}; // only export exports
}
2 changes: 2 additions & 0 deletions ext/net/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ deno_core::extension!(deno_net,
quic::op_quic_send_stream_get_id,
quic::op_quic_send_stream_get_priority,
quic::op_quic_send_stream_set_priority,
quic::webtransport::op_webtransport_accept,
quic::webtransport::op_webtransport_connect,
],
esm = [ "01_net.js", "02_tls.js" ],
lazy_loaded_esm = [ "03_quic.js" ],
Expand Down
Loading