Skip to content

Commit

Permalink
feat(unstable): WebTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jan 6, 2025
1 parent ccd3758 commit 2327813
Show file tree
Hide file tree
Showing 19 changed files with 1,840 additions and 17 deletions.
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.

10 changes: 8 additions & 2 deletions cli/tsc/99_main_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1447,9 +1447,15 @@ delete Object.prototype.__proto__;
options: SNAPSHOT_COMPILE_OPTIONS,
host,
});
const errors = ts.getPreEmitDiagnostics(TS_SNAPSHOT_PROGRAM);
assert(
ts.getPreEmitDiagnostics(TS_SNAPSHOT_PROGRAM).length === 0,
"lib.d.ts files have errors",
errors.length === 0,
`lib.d.ts files have errors:\n${
ts.formatDiagnosticsWithColorAndContext(
errors,
host,
)
}`,
);

// remove this now that we don't need it anymore for warming up tsc
Expand Down
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,
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 @@ -23,6 +23,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"
2 changes: 2 additions & 0 deletions ext/net/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,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

0 comments on commit 2327813

Please sign in to comment.