Skip to content

Commit

Permalink
feat(transport): Split transport code into separate packages
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jun 17, 2021
1 parent df777bc commit 766b9c0
Show file tree
Hide file tree
Showing 76 changed files with 296 additions and 166 deletions.
6 changes: 2 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
"dependencies": {
"@stablelib/cbor": "^1.0.1",
"@tyriar/fibonacci-heap": "^2.0.9",
"adaptive-accrual-failure-detector": "^0.2.0",
"ataraxia-transport": "^0.10.0",
"atvik": "^2.0.0",
"debug": "^4.3.1",
"fast-sha256": "^1.3.0",
"noise-secret-stream": "^3.0.2"
"debug": "^4.3.1"
}
}
5 changes: 5 additions & 0 deletions packages/core/src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { Node } from './Node';

/**
* Message received over a network.
*
* @typeParam MessageTypes -
* definition of types
* @typeParam T -
* keys that are valid, generated via `MessageTypes`
*/
export interface Message<MessageTypes extends object = any, T extends MessageType<MessageTypes> = keyof MessageTypes & string> {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Network.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Event } from 'atvik';
import debug from 'debug';

import { Transport, generateId, encodeId } from 'ataraxia-transport';

import { Exchange } from './exchange/Exchange';
import { Exchanges } from './exchange/Exchanges';
import { generateId, encodeId } from './id';
import { MessageData } from './MessageData';
import { MessageType } from './MessageType';
import { MessageUnion } from './MessageUnion';
import { NetworkNode } from './NetworkNode';
import { Node } from './Node';
import { Topology } from './topology';
import { Transport } from './transport';

/**
* Options that can be provided for `Network`.
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/NetworkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Encoder, Decoder } from '@stablelib/cbor';
import { Event } from 'atvik';
import { debug } from 'debug';

import { encodeId } from './id';
import { encodeId } from 'ataraxia-transport';

import { Message } from './Message';
import { Node } from './Node';
import { Topology } from './topology';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/id/IdMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeId } from './ids';
import { encodeId } from 'ataraxia-transport';

/**
* `Map`-like structure where keys are buffers with identifiers.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/id/IdSet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeId } from './ids';
import { encodeId } from 'ataraxia-transport';

/**
* Set for keeping track of identifiers.
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/id/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './ids';
export * from './IdMap';
export * from './IdSet';
11 changes: 8 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@
* @module ataraxia
*/

export * from './auth';
export {
AuthProvider,
AnonymousAuth,
SharedSecretAuth,
SharedSecretAuthOptions,

Transport
} from 'ataraxia-transport';

export * from './Network';
export * from './Node';
Expand All @@ -58,5 +65,3 @@ export * from './WithNetwork';

export * from './exchange/Exchange';
export * from './RequestReplyHelper';

export * from './BackOff';
11 changes: 7 additions & 4 deletions packages/core/src/test/TestPeer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { AbstractPeer } from '../transport/AbstractPeer';
import { DisconnectReason } from '../transport/DisconnectReason';
import { PeerMessageType, PeerMessage } from '../transport/messages';
import { Peer } from '../transport/Peer';
import {
AbstractPeer,
DisconnectReason,
Peer,
PeerMessage,
PeerMessageType
} from 'ataraxia-transport';

export interface TestPeer extends Peer {
connect(): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/test/TestTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractTransport, Peer } from '../transport';
import { AbstractTransport, Peer } from 'ataraxia-transport';

/**
* Transport suitable for use with tests, only support manual adding of peers.
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/topology/Messaging.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Event } from 'atvik';
import debug from 'debug';

import { sameId, encodeId } from '../id';
import {
Peer,
PeerMessageType,
DataMessage,
DataAckMessage,
DataRejectMessage,
DataMessagePathEntry
} from '../transport';
DataMessagePathEntry,
encodeId,
sameId
} from 'ataraxia-transport';

import { Routing } from './Routing';

Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/topology/Routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { FibonacciHeap } from '@tyriar/fibonacci-heap';
import { Event } from 'atvik';
import debugFactory from 'debug';

import { IdMap, encodeId } from '../id';
import { Peer } from '../transport';
import { encodeId, Peer } from 'ataraxia-transport';

import { IdMap } from '../id';

import { TopologyNode } from './TopologyNode';

Expand Down Expand Up @@ -99,7 +100,10 @@ export class Routing {
this.availableEvent.emit(node);
}
} else {
this.debug('Not available', encodeId(node.id));
if(this.debug.enabled) {
this.debug('Not available', encodeId(node.id));
}

node.previousReachable = false;
this.unavailableEvent.emit(node);

Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/topology/Topology.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Event, SubscriptionHandle } from 'atvik';
import debug from 'debug';

import { IdMap, IdSet, encodeId, sameId } from '../id';
import {
Peer,
NodeRoutingSummary,
NodeSummaryMessage,
PeerMessageType,
NodeRequestMessage,
NodeDetailsMessage,
NodeRoutingDetails
} from '../transport';
NodeRoutingDetails,
encodeId,
sameId
} from 'ataraxia-transport';

import { IdMap, IdSet } from '../id';
import { WithNetwork } from '../WithNetwork';

import { Messaging } from './Messaging';
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/topology/TopologyNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { INode } from '@tyriar/fibonacci-heap';

import { IdSet, encodeId } from '../id';
import { Peer, NodeRoutingDetails } from '../transport';
import { encodeId, Peer, NodeRoutingDetails } from 'ataraxia-transport';

import { IdSet } from '../id';

import { Topology } from './Topology';

Expand Down
19 changes: 0 additions & 19 deletions packages/core/test/id.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/core/test/topology/TopologyTester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-param-reassign */
import { decodeId, encodeId } from '../../src/id';
import { decodeId, encodeId } from 'ataraxia-transport';

import { peersBetween, TestPeer } from '../../src/test';
import { Topology } from '../../src/topology';

Expand Down
5 changes: 0 additions & 5 deletions packages/core/transport/package.json

This file was deleted.

3 changes: 2 additions & 1 deletion packages/hyperswarm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"ataraxia": "^0.10.0",
"ataraxia-transport": "^0.10.0",
"ataraxia-transport-streams": "^0.10.0",
"debug": "^4.3.1",
"hyperswarm": "^2.15.3"
}
Expand Down
14 changes: 9 additions & 5 deletions packages/hyperswarm/src/HyperswarmTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { Duplex } from 'stream';

import hyperswarm, { Swarm } from 'hyperswarm';

import { AuthProvider, WithNetwork } from 'ataraxia';
import { AbstractTransport, EncryptedStreamingPeer, TransportOptions } from 'ataraxia/transport';
import {
AbstractTransport,
AuthProvider,
TransportOptions
} from 'ataraxia-transport';
import { EncryptedStreamingPeer } from 'ataraxia-transport-streams';

/**
* Options that can be used for a Hyperswarm transport.
Expand Down Expand Up @@ -80,7 +84,7 @@ export class HyperswarmTransport extends AbstractTransport {
this.debug('Connecting to a peer, client=', info.client);

this.addPeer(new HyperswarmPeer(
this.network,
this.transportOptions,
this.options.authentication,
socket,
info.client
Expand Down Expand Up @@ -129,12 +133,12 @@ export class HyperswarmTransport extends AbstractTransport {

class HyperswarmPeer extends EncryptedStreamingPeer {
public constructor(
network: WithNetwork,
transportOptions: TransportOptions,
authProviders: ReadonlyArray<AuthProvider>,
socket: Duplex,
client: boolean
) {
super(network, authProviders);
super(transportOptions, authProviders);

this.setStream(socket, client);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"ataraxia": "^0.10.0",
"ataraxia-transport": "^0.10.0",
"ataraxia-transport-streams": "^0.10.0",
"atvik": "^2.0.0",
"debug": "^4.3.1",
"local-machine-network": "^0.3.0"
Expand Down
15 changes: 8 additions & 7 deletions packages/local/src/MachineLocalTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { Duplex } from 'stream';
import { Event, Subscribable } from 'atvik';
import { LowLevelNetwork } from 'local-machine-network';

import { AnonymousAuth, AuthProvider, WithNetwork } from 'ataraxia';
import {
AbstractTransport,
StreamingPeer,
TransportOptions,
DisconnectReason
} from 'ataraxia/transport';
DisconnectReason,
AnonymousAuth,
AuthProvider
} from 'ataraxia-transport';
import { StreamingPeer } from 'ataraxia-transport-streams';

const AUTH = [ new AnonymousAuth() ];

Expand Down Expand Up @@ -153,7 +154,7 @@ export class MachineLocalTransport extends AbstractTransport {
});

const handlePeer = (socket: Socket, server: boolean) => {
const peer = new LocalPeer(this.network, AUTH, socket, ! server);
const peer = new LocalPeer(this.transportOptions, AUTH, socket, ! server);
this.addPeer(peer);
};

Expand All @@ -179,12 +180,12 @@ export class MachineLocalTransport extends AbstractTransport {

class LocalPeer extends StreamingPeer {
public constructor(
network: WithNetwork,
transportOptions: TransportOptions,
authProviders: ReadonlyArray<AuthProvider>,
socket: Duplex,
client: boolean
) {
super(network, authProviders);
super(transportOptions, authProviders);

this.setStream(socket, client);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/tcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"ataraxia": "^0.10.0",
"ataraxia-transport": "^0.10.0",
"ataraxia-transport-streams": "^0.10.0",
"debug": "^4.3.1",
"tinkerhub-discovery": "^0.6.0",
"tinkerhub-mdns": "^0.5.1"
Expand Down
13 changes: 9 additions & 4 deletions packages/tcp/src/TCPClientPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { connect } from 'net';

import { HostAndPort } from 'tinkerhub-discovery';

import { WithNetwork, BackOff, AuthProvider } from 'ataraxia';
import { EncryptedStreamingPeer, DisconnectReason } from 'ataraxia/transport';
import {
AuthProvider,
BackOff,
DisconnectReason,
TransportOptions
} from 'ataraxia-transport';
import { EncryptedStreamingPeer } from 'ataraxia-transport-streams';

/**
* Peer for TCP transport, used for outgoing connections to a server.
Expand All @@ -17,11 +22,11 @@ export class TCPClientPeer extends EncryptedStreamingPeer {
private connectTimeout: any;

public constructor(
network: WithNetwork,
options: TransportOptions,
authProviders: ReadonlyArray<AuthProvider>,
addresses: HostAndPort[]
) {
super(network, authProviders);
super(options, authProviders);

this.backOff = new BackOff({
delay: 100,
Expand Down
11 changes: 7 additions & 4 deletions packages/tcp/src/TCPServerPeer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Socket, } from 'net';

import { WithNetwork, AuthProvider } from 'ataraxia';
import { EncryptedStreamingPeer } from 'ataraxia/transport';
import {
AuthProvider,
TransportOptions
} from 'ataraxia-transport';
import { EncryptedStreamingPeer } from 'ataraxia-transport-streams';

/**
* Peer for TCP transport, used when representing an incoming connection
* from a client.
*/
export class TCPServerPeer extends EncryptedStreamingPeer {
public constructor(
network: WithNetwork,
transportOptions: TransportOptions,
authProviders: ReadonlyArray<AuthProvider>,
socket: Socket
) {
super(network, authProviders);
super(transportOptions, authProviders);

this.debug('Client connected from', socket.remoteAddress);

Expand Down
Loading

0 comments on commit 766b9c0

Please sign in to comment.