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

Commit

Permalink
Merge pull request #56 from little-bear-labs/bugfix/fix-linting-issues
Browse files Browse the repository at this point in the history
Apply auto lint fixer
  • Loading branch information
ddimaria authored Nov 9, 2022
2 parents 2e8da7c + a02c541 commit f15cafc
Show file tree
Hide file tree
Showing 20 changed files with 851 additions and 798 deletions.
44 changes: 0 additions & 44 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/js-test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
flags: electron-main

release:
needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main, test-electron-renderer]
needs: [test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main]
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master' # with #262 - 'refs/heads/${{{ github.default_branch }}}'
steps:
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@
},
"dependencies": {
"@chainsafe/libp2p-noise": "^10.0.0",
"@libp2p/components": "^3.0.2",
"@libp2p/interfaces": "^3.0.2",
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-peer-id": "^1.0.5",
"@libp2p/interface-stream-muxer": "^3.0.0",
"@libp2p/interface-transport": "^2.0.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/multistream-select": "^3.0.2",
"@libp2p/peer-id": "^1.1.15",
"@multiformats/multiaddr": "^11.0.3",
"@protobuf-ts/runtime": "^2.8.0",
Expand All @@ -94,6 +98,7 @@
"multiformats": "^10.0.0",
"multihashes": "^4.0.3",
"p-defer": "^4.0.0",
"timeout-abort-controller": "^3.0.0",
"uint8arraylist": "^2.3.3",
"uint8arrays": "^4.0.2",
"uuid": "^9.0.0"
Expand Down
110 changes: 61 additions & 49 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,123 @@
import { default as createError } from 'err-code';
import { Direction } from '@libp2p/interface-connection';
import errCode from 'err-code'
import { Direction } from '@libp2p/interface-connection'

export class WebRTCTransportError extends Error {
constructor(msg: string) {
super('WebRTC transport error: ' + msg);
this.name = 'WebRTCTransportError';
constructor (msg: string) {
super('WebRTC transport error: ' + msg)
this.name = 'WebRTCTransportError'
}
}

export enum codes {
ERR_ALREADY_ABORTED = 'ERR_ALREADY_ABORTED',
ERR_DATA_CHANNEL = 'ERR_DATA_CHANNEL',
ERR_CONNECTION_CLOSED = 'ERR_CONNECTION_CLOSED',
ERR_HASH_NOT_SUPPORTED = 'ERR_HASH_NOT_SUPPORTED',
ERR_INVALID_MULTIADDR = 'ERR_INVALID_MULTIADDR',
ERR_INVALID_FINGERPRINT = 'ERR_INVALID_FINGERPRINT',
ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS',
ERR_HASH_NOT_SUPPORTED = 'ERR_HASH_NOT_SUPPORTED',
ERR_NOT_IMPLEMENTED = 'ERR_NOT_IMPLEMENTED',
ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS',
ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS',
ERR_CONNECTION_CLOSED = 'ERR_CONNECTION_CLOSED',
}

export class ConnectionClosedError extends WebRTCTransportError {
constructor(state: RTCPeerConnectionState, msg: string) {
super(`peerconnection moved to state: ${state}:` + msg);
this.name = 'WebRTC/ConnectionClosed';
constructor (state: RTCPeerConnectionState, msg: string) {
super(`peerconnection moved to state: ${state}:` + msg)
this.name = 'WebRTC/ConnectionClosed'
}
}

export function connectionClosedError(state: RTCPeerConnectionState, msg: string) {
return createError(new ConnectionClosedError(state, msg), codes.ERR_CONNECTION_CLOSED)
export function connectionClosedError (state: RTCPeerConnectionState, msg: string) {
return errCode(new ConnectionClosedError(state, msg), codes.ERR_CONNECTION_CLOSED)
}

export class InvalidArgumentError extends WebRTCTransportError {
constructor(msg: string) {
super('There was a problem with a provided argument: ' + msg);
this.name = 'WebRTC/InvalidArgumentError';
constructor (msg: string) {
super('There was a problem with a provided argument: ' + msg)
this.name = 'WebRTC/InvalidArgumentError'
}
}

export function unsupportedHashAlgorithm(algorithm: string) {
return createError(new UnsupportedHashAlgorithmError(algorithm), codes.ERR_HASH_NOT_SUPPORTED);
export function unsupportedHashAlgorithm (algorithm: string) {
return errCode(new UnsupportedHashAlgorithmError(algorithm), codes.ERR_HASH_NOT_SUPPORTED)
}

export class UnsupportedHashAlgorithmError extends WebRTCTransportError {
constructor(algo: string) {
let msg = `unsupported hash algorithm: ${algo}`;
super(msg);
this.name = 'WebRTC/UnsupportedHashAlgorithmError';
constructor (algo: string) {
const msg = `unsupported hash algorithm: ${algo}`
super(msg)
this.name = 'WebRTC/UnsupportedHashAlgorithmError'
}
}

export function invalidArgument(msg: string) {
return createError(new InvalidArgumentError(msg), codes.ERR_INVALID_PARAMETERS);
export function invalidArgument (msg: string) {
return errCode(new InvalidArgumentError(msg), codes.ERR_INVALID_PARAMETERS)
}

export class UnimplementedError extends WebRTCTransportError {
constructor(methodName: string) {
super('A method (' + methodName + ') was called though it has been intentionally left unimplemented.');
this.name = 'WebRTC/UnimplementedError';
constructor (methodName: string) {
super('A method (' + methodName + ') was called though it has been intentionally left unimplemented.')
this.name = 'WebRTC/UnimplementedError'
}
}

export function unimplemented (methodName: string) {
return errCode(new UnimplementedError(methodName), codes.ERR_NOT_IMPLEMENTED)
}

export class InvalidFingerprintError extends WebRTCTransportError {
constructor (fingerprint: string, source: string) {
super(`Invalid fingerprint "${fingerprint}" within ${source}`)
this.name = 'WebRTC/InvalidFingerprintError'
}
}

export function unimplemented(methodName: string) {
return createError(new UnimplementedError(methodName), codes.ERR_NOT_IMPLEMENTED);
export function invalidFingerprint (fingerprint: string, source: string) {
return errCode(new InvalidFingerprintError(fingerprint, source), codes.ERR_INVALID_FINGERPRINT)
}

export class InappropriateMultiaddrError extends WebRTCTransportError {
constructor(msg: string) {
super('There was a problem with the Multiaddr which was passed in: ' + msg);
this.name = 'WebRTC/InappropriateMultiaddrError';
constructor (msg: string) {
super('There was a problem with the Multiaddr which was passed in: ' + msg)
this.name = 'WebRTC/InappropriateMultiaddrError'
}
}

export function inappropriateMultiaddr(msg: string) {
return createError(new InappropriateMultiaddrError(msg), codes.ERR_INVALID_MULTIADDR);
export function inappropriateMultiaddr (msg: string) {
return errCode(new InappropriateMultiaddrError(msg), codes.ERR_INVALID_MULTIADDR)
}

export class OperationAbortedError extends WebRTCTransportError {
constructor(context: string, abortReason: string) {
super(`Signalled to abort because (${abortReason}})${context}`);
this.name = 'WebRTC/OperationAbortedError';
constructor (context: string, abortReason: string) {
super(`Signalled to abort because (${abortReason}})${context}`)
this.name = 'WebRTC/OperationAbortedError'
}
}

export function operationAborted(context: string, reason: string) {
return createError(new OperationAbortedError(context, reason), codes.ERR_ALREADY_ABORTED);
export function operationAborted (context: string, reason: string) {
return errCode(new OperationAbortedError(context, reason), codes.ERR_ALREADY_ABORTED)
}

export class DataChannelError extends WebRTCTransportError {
constructor(streamLabel: string, errorMessage: string) {
super(`[stream: ${streamLabel}] data channel error: ${errorMessage}`);
this.name = 'WebRTC/DataChannelError';
constructor (streamLabel: string, errorMessage: string) {
super(`[stream: ${streamLabel}] data channel error: ${errorMessage}`)
this.name = 'WebRTC/DataChannelError'
}
}

export function dataChannelError(streamLabel: string, msg: string) {
return createError(new DataChannelError(streamLabel, msg), codes.ERR_DATA_CHANNEL);
export function dataChannelError (streamLabel: string, msg: string) {
return errCode(new DataChannelError(streamLabel, msg), codes.ERR_DATA_CHANNEL)
}

export class StreamingLimitationError extends WebRTCTransportError {
constructor(msg: string) {
super(msg);
this.name = 'WebRTC/StreamingLimitationError';
constructor (msg: string) {
super(msg)
this.name = 'WebRTC/StreamingLimitationError'
}
}

export function overStreamLimit(dir: Direction, proto: string) {
let code = dir == 'inbound' ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS;
return createError(new StreamingLimitationError(`${dir} stream limit reached for protocol - ${proto}`), code);
export function overStreamLimit (dir: Direction, proto: string) {
const code = dir === 'inbound' ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
return errCode(new StreamingLimitationError(`${dir} stream limit reached for protocol - ${proto}`), code)
}
37 changes: 19 additions & 18 deletions src/maconn.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
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.js";
import { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection'
import { logger } from '@libp2p/logger'
import { Multiaddr } from '@multiformats/multiaddr'
import { Source, Sink } from 'it-stream-types'

const log = logger('libp2p:webrtc:connection');
import { nopSink, nopSource } from './util.js'

type WebRTCMultiaddrConnectionInit = {
peerConnection: RTCPeerConnection;
remoteAddr: Multiaddr;
timeline: MultiaddrConnectionTimeline;
};
const log = logger('libp2p:webrtc:connection')

interface WebRTCMultiaddrConnectionInit {
peerConnection: RTCPeerConnection
remoteAddr: Multiaddr
timeline: MultiaddrConnectionTimeline
}

export class WebRTCMultiaddrConnection implements MultiaddrConnection {
private peerConnection: RTCPeerConnection;
private readonly 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;
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)
async close (err?: Error | undefined): Promise<void> {
log.error('error closing connection', err)
this.peerConnection.close()
}
}
2 changes: 2 additions & 0 deletions src/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ message Message {
enum Flag {
// The sender will no longer send messages on the stream.
FIN = 0;

// 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
39 changes: 19 additions & 20 deletions src/muxer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// 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 {WebRTCStream} from "./stream.js"
import {nopSink, nopSource} from "./util.js"
import { Stream } from '@libp2p/interface-connection'
import { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
import { Source, Sink } from 'it-stream-types'

import { WebRTCStream } from './stream.js'
import { nopSink, nopSource } from './util.js'

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

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

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

export class DataChannelMuxer implements StreamMuxer {
private readonly peerConnection: RTCPeerConnection
readonly protocol: string = "/webrtc"
readonly protocol: string = '/webrtc'
streams: Stream[] = []
init?: StreamMuxerInit
close: (err?: Error | undefined) => void = () => {}
Expand All @@ -30,37 +31,35 @@ export class DataChannelMuxer implements StreamMuxer {
source: Source<Uint8Array> = nopSource;
sink: Sink<Uint8Array, Promise<void>> = nopSink;


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

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

0 comments on commit f15cafc

Please sign in to comment.