Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
fix: remove protobuf-ts and split code into two folders (#162)
Browse files Browse the repository at this point in the history
This module uses both protobuf-ts and protons for protobuf operations. To reduce the bundle size, just use protons for both.

Splits the two transports into `private-to-private` and `private-to-public` folders to better tell what code is used by what.

Module exports are unaffected so this is a non-breaking change.
  • Loading branch information
achingbrain committed May 12, 2023
1 parent 2f5c524 commit 64723a7
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 179 deletions.
6 changes: 3 additions & 3 deletions examples/browser-to-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"test": "npm run build && test-browser-example tests"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.0",
"@libp2p/websockets": "^5.0.3",
"@libp2p/mplex": "^7.0.0",
"@chainsafe/libp2p-noise": "^12.0.0",
"@libp2p/websockets": "^6.0.1",
"@libp2p/mplex": "^8.0.1",
"@libp2p/webrtc": "file:../../",
"@multiformats/multiaddr": "^12.0.0",
"it-pushable": "^3.1.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-to-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "npm run build && test-browser-example tests"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.0",
"@chainsafe/libp2p-noise": "^12.0.0",
"@libp2p/webrtc": "file:../../",
"@multiformats/multiaddr": "^12.0.0",
"it-pushable": "^3.1.0",
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@
]
},
"scripts": {
"generate:proto": "npx protoc --ts_out proto_ts --proto_path src src/*.proto",
"generate:webrtc-direct": "protons src/peer_transport/pb/index.proto",
"generate": "protons src/private-to-private/pb/message.proto src/pb/message.proto",
"build": "aegir build",
"test": "aegir test -t browser",
"test:chrome": "aegir test -t browser --cov",
Expand All @@ -148,7 +147,6 @@
"@libp2p/logger": "^2.0.7",
"@libp2p/peer-id": "^2.0.3",
"@multiformats/multiaddr": "^12.1.2",
"@protobuf-ts/runtime": "^2.9.0",
"abortable-iterator": "^5.0.1",
"detect-browser": "^5.3.0",
"it-length-prefixed": "^9.0.1",
Expand All @@ -167,7 +165,6 @@
"devDependencies": {
"@libp2p/interface-mocks": "^12.0.1",
"@libp2p/peer-id-factory": "^2.0.3",
"@protobuf-ts/protoc": "^2.9.0",
"@types/sinon": "^10.0.14",
"aegir": "^39.0.6",
"it-pair": "^2.0.6",
Expand Down
106 changes: 0 additions & 106 deletions proto_ts/message.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebRTCTransport } from './peer_transport/transport.js'
import { WebRTCDirectTransport, type WebRTCDirectTransportComponents } from './transport.js'
import type { WebRTCTransportComponents, WebRTCTransportInit } from './peer_transport/transport.js'
import { WebRTCTransport } from './private-to-private/transport.js'
import { WebRTCDirectTransport, type WebRTCDirectTransportComponents } from './private-to-public/transport.js'
import type { WebRTCTransportComponents, WebRTCTransportInit } from './private-to-private/transport.js'
import type { Transport } from '@libp2p/interface-transport'

function webRTCDirect (): (components: WebRTCDirectTransportComponents) => Transport {
Expand Down
6 changes: 2 additions & 4 deletions src/message.proto → src/pb/message.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
syntax = "proto2";

package webrtc.pb;
syntax = "proto3";

message Message {
enum Flag {
Expand All @@ -10,7 +8,7 @@ message Message {
// The sender will no longer read messages on the stream. Incoming data is
// being discarded on receipt.
STOP_SENDING = 1;

// The sender abruptly terminates the sending part of the stream. The
// receiver can discard any data that it already received on that stream.
RESET = 2;
Expand Down
92 changes: 92 additions & 0 deletions src/pb/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable import/export */
/* eslint-disable complexity */
/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
/* eslint-disable @typescript-eslint/no-empty-interface */

import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import type { Uint8ArrayList } from 'uint8arraylist'

export interface Message {
flag?: Message.Flag
message?: Uint8Array
}

export namespace Message {
export enum Flag {
FIN = 'FIN',
STOP_SENDING = 'STOP_SENDING',
RESET = 'RESET'
}

enum __FlagValues {
FIN = 0,
STOP_SENDING = 1,
RESET = 2
}

export namespace Flag {
export const codec = (): Codec<Flag> => {
return enumeration<Flag>(__FlagValues)
}
}

let _codec: Codec<Message>

export const codec = (): Codec<Message> => {
if (_codec == null) {
_codec = message<Message>((obj, w, opts = {}) => {
if (opts.lengthDelimited !== false) {
w.fork()
}

if (obj.flag != null) {
w.uint32(8)
Message.Flag.codec().encode(obj.flag, w)
}

if (obj.message != null) {
w.uint32(18)
w.bytes(obj.message)
}

if (opts.lengthDelimited !== false) {
w.ldelim()
}
}, (reader, length) => {
const obj: any = {}

const end = length == null ? reader.len : reader.pos + length

while (reader.pos < end) {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
obj.flag = Message.Flag.codec().decode(reader)
break
case 2:
obj.message = reader.bytes()
break
default:
reader.skipType(tag & 7)
break
}
}

return obj
})
}

return _codec
}

export const encode = (obj: Partial<Message>): Uint8Array => {
return encodeMessage(obj, Message.codec())
}

export const decode = (buf: Uint8Array | Uint8ArrayList): Message => {
return decodeMessage(buf, Message.codec())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { abortableDuplex } from 'abortable-iterator'
import { pbStream } from 'it-pb-stream'
import pDefer, { type DeferredPromise } from 'p-defer'
import { DataChannelMuxerFactory } from '../muxer.js'
import * as pb from './pb/index.js'
import { Message } from './pb/message.js'
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
import type { Stream } from '@libp2p/interface-connection'
import type { IncomingStreamData } from '@libp2p/interface-registrar'
Expand All @@ -17,7 +17,7 @@ export type IncomingStreamOpts = { rtcConfiguration?: RTCConfiguration } & Incom

export async function handleIncomingStream ({ rtcConfiguration, stream: rawStream }: IncomingStreamOpts): Promise<{ pc: RTCPeerConnection, muxerFactory: StreamMuxerFactory, remoteAddress: string }> {
const signal = AbortSignal.timeout(DEFAULT_TIMEOUT)
const stream = pbStream(abortableDuplex(rawStream, signal)).pb(pb.Message)
const stream = pbStream(abortableDuplex(rawStream, signal)).pb(Message)
const pc = new RTCPeerConnection(rtcConfiguration)
const muxerFactory = new DataChannelMuxerFactory(pc)

Expand All @@ -30,7 +30,7 @@ export async function handleIncomingStream ({ rtcConfiguration, stream: rawStrea
answerSentPromise.promise.then(
() => {
stream.write({
type: pb.Message.Type.ICE_CANDIDATE,
type: Message.Type.ICE_CANDIDATE,
data: (candidate != null) ? JSON.stringify(candidate.toJSON()) : ''
})
},
Expand All @@ -44,7 +44,7 @@ export async function handleIncomingStream ({ rtcConfiguration, stream: rawStrea

// read an SDP offer
const pbOffer = await stream.read()
if (pbOffer.type !== pb.Message.Type.SDP_OFFER) {
if (pbOffer.type !== Message.Type.SDP_OFFER) {
throw new Error(`expected message type SDP_OFFER, received: ${pbOffer.type ?? 'undefined'} `)
}
const offer = new RTCSessionDescription({
Expand All @@ -64,7 +64,7 @@ export async function handleIncomingStream ({ rtcConfiguration, stream: rawStrea
throw new Error('Failed to create answer')
})
// write the answer to the remote
stream.write({ type: pb.Message.Type.SDP_ANSWER, data: answer.sdp })
stream.write({ type: Message.Type.SDP_ANSWER, data: answer.sdp })

await pc.setLocalDescription(answer).catch(err => {
log.error('could not execute setLocalDescription', err)
Expand All @@ -89,7 +89,7 @@ export interface ConnectOptions {
}

export async function initiateConnection ({ rtcConfiguration, signal, stream: rawStream }: ConnectOptions): Promise<{ pc: RTCPeerConnection, muxerFactory: StreamMuxerFactory, remoteAddress: string }> {
const stream = pbStream(abortableDuplex(rawStream, signal)).pb(pb.Message)
const stream = pbStream(abortableDuplex(rawStream, signal)).pb(Message)
// setup peer connection
const pc = new RTCPeerConnection(rtcConfiguration)
const muxerFactory = new DataChannelMuxerFactory(pc)
Expand All @@ -107,14 +107,14 @@ export async function initiateConnection ({ rtcConfiguration, signal, stream: ra
// peer
pc.onicecandidate = ({ candidate }) => {
stream.write({
type: pb.Message.Type.ICE_CANDIDATE,
type: Message.Type.ICE_CANDIDATE,
data: (candidate != null) ? JSON.stringify(candidate.toJSON()) : ''
})
}
// create an offer
const offerSdp = await pc.createOffer()
// write the offer to the stream
stream.write({ type: pb.Message.Type.SDP_OFFER, data: offerSdp.sdp })
stream.write({ type: Message.Type.SDP_OFFER, data: offerSdp.sdp })
// set offer as local description
await pc.setLocalDescription(offerSdp).catch(err => {
log.error('could not execute setLocalDescription', err)
Expand All @@ -123,7 +123,7 @@ export async function initiateConnection ({ rtcConfiguration, signal, stream: ra

// read answer
const answerMessage = await stream.read()
if (answerMessage.type !== pb.Message.Type.SDP_ANSWER) {
if (answerMessage.type !== Message.Type.SDP_ANSWER) {
throw new Error('remote should send an SDP answer')
}

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export namespace Message {
return _codec
}

export const encode = (obj: Message): Uint8Array => {
export const encode = (obj: Partial<Message>): Uint8Array => {
return encodeMessage(obj, Message.codec())
}

Expand Down
File renamed without changes.
13 changes: 5 additions & 8 deletions src/peer_transport/util.ts → src/private-to-private/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { logger } from '@libp2p/logger'
import { detect } from 'detect-browser'
import * as pb from './pb/index.js'
import { isFirefox } from '../util.js'
import { Message } from './pb/message.js'
import type { DeferredPromise } from 'p-defer'

const browser = detect()
export const isFirefox = ((browser != null) && browser.name === 'firefox')

interface MessageStream {
read: () => Promise<pb.Message>
write: (d: pb.Message) => void | Promise<void>
read: () => Promise<Message>
write: (d: Message) => void | Promise<void>
}

const log = logger('libp2p:webrtc:peer:util')
Expand All @@ -19,7 +16,7 @@ export const readCandidatesUntilConnected = async (connectedPromise: DeferredPro
// check if readResult is a message
if (readResult instanceof Object) {
const message = readResult
if (message.type !== pb.Message.Type.ICE_CANDIDATE) {
if (message.type !== Message.Type.ICE_CANDIDATE) {
throw new Error('expected only ice candidates')
}
// end of candidates has been signalled
Expand Down
File renamed without changes.
Loading

0 comments on commit 64723a7

Please sign in to comment.