Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 26 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

interface WebTransportConfig {
serverCertificateHashes?: Array<{ algorithm: string, value: Uint8Array }>
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebTransportBidirectionalStream
*/
interface WebTransportBidirectionalStream {
readable: ReadableStream
writable: WritableStream
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebTransport
*/
interface GlobalThisWebTransport extends EventTarget {
closed: Promise<any>
datagrams: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>
incomingBidirectionalStreams: ReadableStream<WebTransportBidirectionalStream>
incomingUnidirectionalStreams: ReadableStream
ready: Promise<any>
close: (options?: { closeCode: number, reason: string }) => void
createBidirectionalStream: () => Promise<Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
createUnidirectionalStream: () => Promise<Stream>
}
// eslint-disable-next-line no-var, @typescript-eslint/no-unused-vars
var WebTransport: new (url: string, config: WebTransportConfig) => GlobalThisWebTransport
13 changes: 4 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import type { StreamMuxerFactory, StreamMuxerInit, StreamMuxer } from '@libp2p/i
import { Uint8ArrayList } from 'uint8arraylist'

const log = logger('libp2p:webtransport')
declare global {
interface Window {
WebTransport: any
}
}

// @ts-expect-error - Not easy to combine these types.
const multibaseDecoder = Object.values(bases).map(b => b.decoder).reduce((d, b) => d.or(b))
Expand Down Expand Up @@ -299,7 +294,7 @@ class WebTransport implements Transport {
throw new Error('Expected multiaddr to contain certhashes')
}

const wt = new window.WebTransport(`${url}/.well-known/libp2p-webtransport?type=noise`, {
const wt = new globalThis.WebTransport(`${url}/.well-known/libp2p-webtransport?type=noise`, {
serverCertificateHashes: certhashes.map(certhash => ({
algorithm: 'sha-256',
value: certhash.digest
Expand Down Expand Up @@ -349,7 +344,7 @@ class WebTransport implements Transport {
return await options.upgrader.upgradeOutbound(maConn, { skipEncryption: true, muxerFactory: this.webtransportMuxer(wt), skipProtection: true })
}

async authenticateWebTransport (wt: typeof window.WebTransport, localPeer: PeerId, remotePeer: PeerId, certhashes: Array<MultihashDigest<number>>): Promise<boolean> {
async authenticateWebTransport (wt: InstanceType<typeof globalThis.WebTransport>, localPeer: PeerId, remotePeer: PeerId, certhashes: Array<MultihashDigest<number>>): Promise<boolean> {
const stream = await wt.createBidirectionalStream()
const writer = stream.writable.getWriter()
const reader = stream.readable.getReader()
Expand Down Expand Up @@ -390,7 +385,7 @@ class WebTransport implements Transport {
return true
}

webtransportMuxer (wt: typeof window.WebTransport): StreamMuxerFactory {
webtransportMuxer (wt: InstanceType<typeof globalThis.WebTransport>): StreamMuxerFactory {
let streamIDCounter = 0
const config = this.config
return {
Expand All @@ -411,7 +406,7 @@ class WebTransport implements Transport {
const reader = wt.incomingBidirectionalStreams.getReader()
while (true) {
const { done, value: wtStream } = await reader.read()
if (done === true) {
if (done) {
break
}
if (activeStreams.length >= config.maxInboundStreams) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"lib": ["ES2021", "ES2021.Promise", "ES2021.String", "ES2020.BigInt", "DOM", "DOM.Iterable", "WebWorker"],
},
"include": [
"src",
Expand Down