Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
"devDependencies": {
"@libp2p/interface-mocks": "^4.0.1",
"@libp2p/peer-id-factory": "^1.0.18",
"@multiformats/multiaddr": "^10.4.1",
"@multiformats/multiaddr": "10.4.3",
"@types/uuid": "^8.3.4",
"@typescript-eslint/parser": "^5.32.0",
"aegir": "^37.4.6",
"chai-bytes": "^0.1.2",
"it-all": "^1.0.6",
"it-first": "^1.0.7",
"libp2p": "file:../js-libp2p",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"typescript": "^4.7.4",
Expand All @@ -57,12 +59,13 @@
"@libp2p/components": "^2.0.3",
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-registrar": "^2.0.3",
"@libp2p/interface-transport": "^1.0.3",
"@libp2p/interface-stream-muxer": "^2.0.2",
"@libp2p/interface-transport": "file:../js-libp2p-interfaces/packages/interface-transport",
"@libp2p/interfaces": "^3.0.3",
"@libp2p/logger": "^2.0.0",
"@libp2p/multistream-select": "^3.0.0",
"@libp2p/peer-id": "^1.1.15",
"@multiformats/multiaddr": "^10.4.0",
"@multiformats/multiaddr": "10.4.3",
"@protobuf-ts/plugin": "^2.8.0",
"@protobuf-ts/protoc": "^2.8.0",
"@protobuf-ts/runtime": "^2.8.0",
Expand Down
254 changes: 0 additions & 254 deletions src/connection.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/maconn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {MultiaddrConnection, MultiaddrConnectionTimeline} from "@libp2p/interface-connection";
import { logger } from '@libp2p/logger';
import {Multiaddr} from "@multiformats/multiaddr";
import {Source, Sink} from "it-stream-types";
import {nopSink, nopSource} from "./util";

const log = logger('libp2p:webrtc:connection');

type WebRTCMultiaddrConnectionInit = {
peerConnection: RTCPeerConnection;
remoteAddr: Multiaddr;
timeline: MultiaddrConnectionTimeline;
};

export class WebRTCMultiaddrConnection implements MultiaddrConnection {
private peerConnection: RTCPeerConnection;
remoteAddr: Multiaddr;
timeline: MultiaddrConnectionTimeline;

source: Source<Uint8Array> = nopSource
sink: Sink<Uint8Array, Promise<void>> = nopSink;

constructor(init: WebRTCMultiaddrConnectionInit) {
this.remoteAddr = init.remoteAddr;
this.timeline = init.timeline;
this.peerConnection = init.peerConnection;
}

async close(err?: Error | undefined): Promise<void> {
log.error("error closing connection", err)
this.peerConnection.close()
}
}
72 changes: 72 additions & 0 deletions src/muxer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// import {Components} from "@libp2p/components"
import {Stream} from "@libp2p/interface-connection"
import {StreamMuxer, StreamMuxerFactory, StreamMuxerInit} from "@libp2p/interface-stream-muxer"
import {Source, Sink} from "it-stream-types"
import {v4} from "uuid"
import {WebRTCStream} from "./stream"
import {nopSink, nopSource} from "./util"

export class DataChannelMuxerFactory implements StreamMuxerFactory {
private peerConnection: RTCPeerConnection
protocol: string = '/webrtc'

constructor(peerConnection: RTCPeerConnection) {
this.peerConnection = peerConnection
}

createStreamMuxer(init?: StreamMuxerInit | undefined): StreamMuxer {
return new DataChannelMuxer(this.peerConnection, init)
}
}

export class DataChannelMuxer implements StreamMuxer {
private readonly peerConnection: RTCPeerConnection
readonly protocol: string = "/webrtc"
streams: Stream[] = []
init?: StreamMuxerInit
close: (err?: Error | undefined) => void = () => {}

// nop source and sink, since the transport natively supports
// multiplexing
source: Source<Uint8Array> = nopSource;
sink: Sink<Uint8Array, Promise<void>> = nopSink;


constructor(peerConnection: RTCPeerConnection, init?: StreamMuxerInit) {
this.init = init
this.peerConnection = peerConnection
this.peerConnection.ondatachannel = ({channel}) => {
const stream = new WebRTCStream({
channel,
stat: {
direction: 'inbound',
timeline: {
open: 0,
}
},
closeCb: init?.onStreamEnd
})
if (init?.onIncomingStream) {
init.onIncomingStream!(stream)
}
}
}

newStream(name?: string | undefined): Stream {
const streamName = name || v4();
const channel = this.peerConnection.createDataChannel(streamName)
const stream = new WebRTCStream({
channel,
stat: {
direction: 'outbound',
timeline: {
open: 0,
},
},
closeCb: this.init?.onStreamEnd
})
return stream
}
}

// export {}
Loading