Skip to content

Commit cd4e5db

Browse files
committed
quic-transport: port to Node with webtransport-bun
1 parent 98636d3 commit cd4e5db

5 files changed

Lines changed: 22 additions & 3 deletions

File tree

pkg/quic-transport/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This package is part of [NDNts](https://yoursunny.com/p/NDNts/), Named Data Netw
44

55
This package implements an HTTP/3 client transport using [WebTransport API](https://developer.mozilla.org/en-US/docs/Web/API/WebTransport).
66
It is designed to work with [NDN HTTP/3 WebTransport gateway](https://github.com/yoursunny/NDN-webtrans).
7+
It works in both Node.js and browser.
78

89
You can create a forwarder face that uses HTTP/3 transport with `H3Transport.createFace()` function.
910
To create an HTTP/3 transport without wrapping into L3Face, use `H3Transport.connect()` function.

pkg/quic-transport/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"@ndn/l3face": "workspace:*",
3030
"@ndn/util": "workspace:*",
31+
"@webtransport-bun/webtransport": "0.3.0",
3132
"tslib": "^2.8.1"
3233
}
3334
}

pkg/quic-transport/src/h3-transport.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { L3Face, rxFromPacketIterable, Transport } from "@ndn/l3face";
22
import { delay } from "@ndn/util";
33

4+
import { makeWebTransport, supported } from "./wt_node";
5+
46
/** HTTP/3 transport. */
57
export class H3Transport extends Transport {
6-
/** Whether current browser supports WebTransport. */
7-
public static readonly supported: boolean = !!globalThis.WebTransport;
8+
/** Whether current environment supports WebTransport. */
9+
public static readonly supported: boolean = supported;
810

911
/**
1012
* Create a transport and connect to remote endpoint.
@@ -13,7 +15,7 @@ export class H3Transport extends Transport {
1315
*/
1416
public static async connect(uri: string, opts: H3Transport.Options = {}): Promise<H3Transport> {
1517
const { connectTimeout = 10000, ...wtOpts } = opts;
16-
const tr = new WebTransport(uri, wtOpts);
18+
const tr = makeWebTransport(uri, wtOpts);
1719
void tr.closed.catch(() => undefined); // eslint-disable-line promise/prefer-await-to-then
1820
const isTimeout = await Promise.race([
1921
tr.ready,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const supported = !!globalThis.WebTransport;
2+
3+
export function makeWebTransport(uri: string, opts: WebTransportOptions): WebTransport {
4+
return new WebTransport(uri, opts);
5+
}

pkg/quic-transport/src/wt_node.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { WebTransport as bunWebTransport } from "@webtransport-bun/webtransport";
2+
3+
export const supported = true;
4+
5+
export function makeWebTransport(uri: string, opts: WebTransportOptions): WebTransport {
6+
void opts;
7+
return new bunWebTransport(uri, {
8+
strictW3CErrors: true,
9+
}) as unknown as WebTransport;
10+
}

0 commit comments

Comments
 (0)