diff --git a/packages/core/package.json b/packages/core/package.json index 505ebe9..557df39 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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" } } diff --git a/packages/core/src/Message.ts b/packages/core/src/Message.ts index f8f00a8..db4052c 100644 --- a/packages/core/src/Message.ts +++ b/packages/core/src/Message.ts @@ -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 = keyof MessageTypes & string> { /** diff --git a/packages/core/src/Network.ts b/packages/core/src/Network.ts index ccc211c..677cb48 100644 --- a/packages/core/src/Network.ts +++ b/packages/core/src/Network.ts @@ -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`. diff --git a/packages/core/src/NetworkNode.ts b/packages/core/src/NetworkNode.ts index bdf3377..1d7bb80 100644 --- a/packages/core/src/NetworkNode.ts +++ b/packages/core/src/NetworkNode.ts @@ -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'; diff --git a/packages/core/src/id/IdMap.ts b/packages/core/src/id/IdMap.ts index 63c3e9a..c50b7a1 100644 --- a/packages/core/src/id/IdMap.ts +++ b/packages/core/src/id/IdMap.ts @@ -1,4 +1,4 @@ -import { encodeId } from './ids'; +import { encodeId } from 'ataraxia-transport'; /** * `Map`-like structure where keys are buffers with identifiers. diff --git a/packages/core/src/id/IdSet.ts b/packages/core/src/id/IdSet.ts index be85891..076921f 100644 --- a/packages/core/src/id/IdSet.ts +++ b/packages/core/src/id/IdSet.ts @@ -1,4 +1,4 @@ -import { encodeId } from './ids'; +import { encodeId } from 'ataraxia-transport'; /** * Set for keeping track of identifiers. diff --git a/packages/core/src/id/index.ts b/packages/core/src/id/index.ts index 9a1327e..e2bfff8 100644 --- a/packages/core/src/id/index.ts +++ b/packages/core/src/id/index.ts @@ -1,3 +1,2 @@ -export * from './ids'; export * from './IdMap'; export * from './IdSet'; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 73314b6..1d97b12 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -44,7 +44,14 @@ * @module ataraxia */ -export * from './auth'; +export { + AuthProvider, + AnonymousAuth, + SharedSecretAuth, + SharedSecretAuthOptions, + + Transport +} from 'ataraxia-transport'; export * from './Network'; export * from './Node'; @@ -58,5 +65,3 @@ export * from './WithNetwork'; export * from './exchange/Exchange'; export * from './RequestReplyHelper'; - -export * from './BackOff'; diff --git a/packages/core/src/test/TestPeer.ts b/packages/core/src/test/TestPeer.ts index fddf5d8..b57663d 100644 --- a/packages/core/src/test/TestPeer.ts +++ b/packages/core/src/test/TestPeer.ts @@ -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; diff --git a/packages/core/src/test/TestTransport.ts b/packages/core/src/test/TestTransport.ts index 8e66026..3354104 100644 --- a/packages/core/src/test/TestTransport.ts +++ b/packages/core/src/test/TestTransport.ts @@ -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. diff --git a/packages/core/src/topology/Messaging.ts b/packages/core/src/topology/Messaging.ts index 2192e53..092f8d0 100644 --- a/packages/core/src/topology/Messaging.ts +++ b/packages/core/src/topology/Messaging.ts @@ -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'; diff --git a/packages/core/src/topology/Routing.ts b/packages/core/src/topology/Routing.ts index 5e8e12f..8202105 100644 --- a/packages/core/src/topology/Routing.ts +++ b/packages/core/src/topology/Routing.ts @@ -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'; @@ -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); diff --git a/packages/core/src/topology/Topology.ts b/packages/core/src/topology/Topology.ts index 095ab25..fc0c8cc 100644 --- a/packages/core/src/topology/Topology.ts +++ b/packages/core/src/topology/Topology.ts @@ -1,7 +1,6 @@ import { Event, SubscriptionHandle } from 'atvik'; import debug from 'debug'; -import { IdMap, IdSet, encodeId, sameId } from '../id'; import { Peer, NodeRoutingSummary, @@ -9,8 +8,12 @@ import { 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'; diff --git a/packages/core/src/topology/TopologyNode.ts b/packages/core/src/topology/TopologyNode.ts index d08a5b6..d35e6e9 100644 --- a/packages/core/src/topology/TopologyNode.ts +++ b/packages/core/src/topology/TopologyNode.ts @@ -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'; diff --git a/packages/core/test/id.test.ts b/packages/core/test/id.test.ts deleted file mode 100644 index 75222a4..0000000 --- a/packages/core/test/id.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { generateId, encodeId } from '../src/id'; - -describe('Identifiers', function() { - it('Can generate random id', function() { - generateId(); - }); - - it('Fairly unique id test', function() { - const set = new Set(); - for(let i = 0; i < 1000; i++) { - const id = encodeId(generateId()); - if(set.has(id)) { - throw new Error('Duplicate id generated: ' + id); - } - - set.add(id); - } - }); -}); diff --git a/packages/core/test/topology/TopologyTester.ts b/packages/core/test/topology/TopologyTester.ts index 769ca2f..18b8125 100644 --- a/packages/core/test/topology/TopologyTester.ts +++ b/packages/core/test/topology/TopologyTester.ts @@ -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'; diff --git a/packages/core/transport/package.json b/packages/core/transport/package.json deleted file mode 100644 index 61211fc..0000000 --- a/packages/core/transport/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "main": "../dist/cjs/transport/index.js", - "module": "../dist/esm/transport/index.js", - "types": "../dist/types/transport/index.d.ts" -} diff --git a/packages/hyperswarm/package.json b/packages/hyperswarm/package.json index 02274c9..c1856f2 100644 --- a/packages/hyperswarm/package.json +++ b/packages/hyperswarm/package.json @@ -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" } diff --git a/packages/hyperswarm/src/HyperswarmTransport.ts b/packages/hyperswarm/src/HyperswarmTransport.ts index f40bc49..dc7c05c 100644 --- a/packages/hyperswarm/src/HyperswarmTransport.ts +++ b/packages/hyperswarm/src/HyperswarmTransport.ts @@ -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. @@ -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 @@ -129,12 +133,12 @@ export class HyperswarmTransport extends AbstractTransport { class HyperswarmPeer extends EncryptedStreamingPeer { public constructor( - network: WithNetwork, + transportOptions: TransportOptions, authProviders: ReadonlyArray, socket: Duplex, client: boolean ) { - super(network, authProviders); + super(transportOptions, authProviders); this.setStream(socket, client); } diff --git a/packages/local/package.json b/packages/local/package.json index 2d082df..e2884a9 100644 --- a/packages/local/package.json +++ b/packages/local/package.json @@ -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" diff --git a/packages/local/src/MachineLocalTransport.ts b/packages/local/src/MachineLocalTransport.ts index ec1f7dd..48681c0 100644 --- a/packages/local/src/MachineLocalTransport.ts +++ b/packages/local/src/MachineLocalTransport.ts @@ -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() ]; @@ -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); }; @@ -179,12 +180,12 @@ export class MachineLocalTransport extends AbstractTransport { class LocalPeer extends StreamingPeer { public constructor( - network: WithNetwork, + transportOptions: TransportOptions, authProviders: ReadonlyArray, socket: Duplex, client: boolean ) { - super(network, authProviders); + super(transportOptions, authProviders); this.setStream(socket, client); } diff --git a/packages/tcp/package.json b/packages/tcp/package.json index b7229a6..5f7ef70 100644 --- a/packages/tcp/package.json +++ b/packages/tcp/package.json @@ -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" diff --git a/packages/tcp/src/TCPClientPeer.ts b/packages/tcp/src/TCPClientPeer.ts index 689da3d..f430873 100644 --- a/packages/tcp/src/TCPClientPeer.ts +++ b/packages/tcp/src/TCPClientPeer.ts @@ -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. @@ -17,11 +22,11 @@ export class TCPClientPeer extends EncryptedStreamingPeer { private connectTimeout: any; public constructor( - network: WithNetwork, + options: TransportOptions, authProviders: ReadonlyArray, addresses: HostAndPort[] ) { - super(network, authProviders); + super(options, authProviders); this.backOff = new BackOff({ delay: 100, diff --git a/packages/tcp/src/TCPServerPeer.ts b/packages/tcp/src/TCPServerPeer.ts index 8387387..da909c6 100644 --- a/packages/tcp/src/TCPServerPeer.ts +++ b/packages/tcp/src/TCPServerPeer.ts @@ -1,7 +1,10 @@ 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 @@ -9,11 +12,11 @@ import { EncryptedStreamingPeer } from 'ataraxia/transport'; */ export class TCPServerPeer extends EncryptedStreamingPeer { public constructor( - network: WithNetwork, + transportOptions: TransportOptions, authProviders: ReadonlyArray, socket: Socket ) { - super(network, authProviders); + super(transportOptions, authProviders); this.debug('Client connected from', socket.remoteAddress); diff --git a/packages/tcp/src/TCPTransport.ts b/packages/tcp/src/TCPTransport.ts index 07bc198..10538c5 100644 --- a/packages/tcp/src/TCPTransport.ts +++ b/packages/tcp/src/TCPTransport.ts @@ -2,8 +2,7 @@ import { createServer, Server } from 'net'; import { ServicePublisher, ServiceDiscovery, MultiAddressService, HostAndPort } from 'tinkerhub-discovery'; -import { AuthProvider } from 'ataraxia'; -import { AbstractTransport, TransportOptions } from 'ataraxia/transport'; +import { AbstractTransport, AuthProvider, encodeId, TransportOptions } from 'ataraxia-transport'; import { TCPClientPeer } from './TCPClientPeer'; import { TCPPeerDiscovery } from './TCPPeerDiscovery'; @@ -92,7 +91,7 @@ export class TCPTransport extends AbstractTransport { this.server.on('connection', socket => { const peer = new TCPServerPeer( - this.network, + this.transportOptions, this.authProviders, socket ); @@ -115,7 +114,7 @@ export class TCPTransport extends AbstractTransport { if(this.discovery && this.discovery.publish) { // A discovery is available, ask it to publish the service this.servicePublisher = this.discovery.publish({ - networkId: this.network.networkId, + networkId: encodeId(this.transportOptions.networkId), networkName: options.networkName, port: this._port }); @@ -130,7 +129,7 @@ export class TCPTransport extends AbstractTransport { } if(this.discovery && this.discovery.newDiscovery) { - const encodedId = this.network.networkId; + const encodedId = encodeId(this.transportOptions.networkId); // Attempt to start a discovery to find peers this.serviceDiscovery = this.discovery.newDiscovery({ @@ -223,7 +222,7 @@ export class TCPTransport extends AbstractTransport { private connectToPeer(addresses: HostAndPort[]) { const peer = new TCPClientPeer( - this.network, + this.transportOptions, this.authProviders, addresses ); diff --git a/packages/transport-streams/package.json b/packages/transport-streams/package.json new file mode 100644 index 0000000..dd7799e --- /dev/null +++ b/packages/transport-streams/package.json @@ -0,0 +1,26 @@ +{ + "name": "ataraxia-transport-streams", + "version": "0.10.0", + "description": "Utilities for Ataraxia transports that use Node streams.", + "license": "MIT", + "repository": "https://github.com/aholstenson/ataraxia/tree/master/packages/transport-streams", + "keywords": [ + "ataraxia" + ], + "main": "./dist/cjs/index.js", + "module": "./dist/esm/index.js", + "types": "./dist/types/index.d.ts", + "sideEffects": false, + "scripts": { + "lint": "tslint --project tsconfig.build.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", + "build": "tsc --project tsconfig.build.json --module commonjs --outDir dist/cjs", + "prebuild": "rimraf dist", + "prepublishOnly": "npm run build" + }, + "dependencies": { + "@stablelib/cbor": "^1.0.1", + "ataraxia-transport": "^0.10.0", + "debug": "^4.3.1", + "noise-secret-stream": "^3.0.2" + } +} diff --git a/packages/core/src/transport/EncryptedStreamingPeer.ts b/packages/transport-streams/src/EncryptedStreamingPeer.ts similarity index 100% rename from packages/core/src/transport/EncryptedStreamingPeer.ts rename to packages/transport-streams/src/EncryptedStreamingPeer.ts diff --git a/packages/core/src/transport/StreamingPeer.ts b/packages/transport-streams/src/StreamingPeer.ts similarity index 94% rename from packages/core/src/transport/StreamingPeer.ts rename to packages/transport-streams/src/StreamingPeer.ts index c4fdfe8..447956d 100644 --- a/packages/core/src/transport/StreamingPeer.ts +++ b/packages/transport-streams/src/StreamingPeer.ts @@ -1,10 +1,13 @@ import { Duplex } from 'stream'; -import { AbstractPeer } from './AbstractPeer'; -import { encodePeerPacket, PeerPacketDecodingStream } from './binary'; -import { DisconnectReason } from './DisconnectReason'; -import { PeerMessage } from './messages/PeerMessage'; -import { PeerMessageType } from './messages/PeerMessageType'; +import { + AbstractPeer, + DisconnectReason, + PeerMessage, + PeerMessageType +} from 'ataraxia-transport'; + +import { encodePeerPacket, PeerPacketDecodingStream } from './binaryStreams'; /** * Peer that connects via a binary streaming protocol. diff --git a/packages/core/src/transport/binary/binaryStreams.ts b/packages/transport-streams/src/binaryStreams.ts similarity index 94% rename from packages/core/src/transport/binary/binaryStreams.ts rename to packages/transport-streams/src/binaryStreams.ts index dd71952..a263b74 100644 --- a/packages/core/src/transport/binary/binaryStreams.ts +++ b/packages/transport-streams/src/binaryStreams.ts @@ -1,9 +1,11 @@ import { Transform, TransformCallback } from 'stream'; -import { PeerMessageType, PeerMessage } from '../messages'; - -import { decodeBinaryPeerMessage } from './decodeBinaryPeerMessage'; -import { encodeBinaryPeerMessage } from './encodeBinaryPeerMessage'; +import { + decodeBinaryPeerMessage, + encodeBinaryPeerMessage, + PeerMessage, + PeerMessageType +} from 'ataraxia-transport'; const ADDITIONAL_INFO_8_BITS = 24; const ADDITIONAL_INFO_16_BITS = 25; diff --git a/packages/transport-streams/src/index.ts b/packages/transport-streams/src/index.ts new file mode 100644 index 0000000..edb4e38 --- /dev/null +++ b/packages/transport-streams/src/index.ts @@ -0,0 +1,10 @@ +/* eslint-disable tsdoc/syntax,jsdoc/require-description */ +/** + * Utilities for transports that use Node streams. + * + * @module ataraxia-transport-streams + */ +export * from './binaryStreams'; + +export * from './StreamingPeer'; +export * from './EncryptedStreamingPeer'; diff --git a/packages/core/src/noise-secret-stream.d.ts b/packages/transport-streams/src/noise-secret-stream.d.ts similarity index 100% rename from packages/core/src/noise-secret-stream.d.ts rename to packages/transport-streams/src/noise-secret-stream.d.ts diff --git a/packages/transport-streams/tsconfig.build.json b/packages/transport-streams/tsconfig.build.json new file mode 100644 index 0000000..ef83010 --- /dev/null +++ b/packages/transport-streams/tsconfig.build.json @@ -0,0 +1,13 @@ + +{ + "extends": "../../tsconfig.settings.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": "./src", + "outDir": "dist/cjs", + "declarationDir": "dist/types", + }, + "include": [ + "src" + ] +} diff --git a/packages/transport/README.md b/packages/transport/README.md new file mode 100644 index 0000000..dcdf5e2 --- /dev/null +++ b/packages/transport/README.md @@ -0,0 +1,8 @@ +# ataraxia-transport + +[![npm version](https://img.shields.io/npm/v/ataraxia-transport)](https://www.npmjs.com/package/ataraxia-transport) +[![Dependencies](https://img.shields.io/librariesio/release/npm/ataraxia-transport)](https://libraries.io/npm/ataraxia-transport) +[![Typedoc](https://img.shields.io/badge/typedoc-ataraxia--transport-%23fff)](https://aholstenson.github.io/ataraxia/modules/ataraxia_transport.html) + +Shared code that is used to implement transports for an [Ataraxia](https://github.com/aholstenson/ataraxia) +network. diff --git a/packages/transport/jest.config.js b/packages/transport/jest.config.js new file mode 100644 index 0000000..9b370f9 --- /dev/null +++ b/packages/transport/jest.config.js @@ -0,0 +1,4 @@ +module.exports = { + rootDir: '.', + ...require('../../jest.config') +} diff --git a/packages/transport/package.json b/packages/transport/package.json new file mode 100644 index 0000000..33697f1 --- /dev/null +++ b/packages/transport/package.json @@ -0,0 +1,32 @@ +{ + "name": "ataraxia-transport", + "version": "0.10.0", + "description": "Base for for connecting to and communicating with peers in an Ataraxia network", + "license": "MIT", + "repository": "https://github.com/aholstenson/ataraxia/tree/master/packages/transport", + "keywords": [ + "ataraxia" + ], + "main": "./dist/cjs/index.js", + "module": "./dist/esm/index.js", + "types": "./dist/types/index.d.ts", + "sideEffects": false, + "scripts": { + "test": "jest", + "test:watch": "jest --watch", + "ci": "npm run coverage && npm run lint", + "coverage": "jest --coverage", + "lint": "tslint --project tsconfig.build.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", + "build": "tsc --project tsconfig.build.json --module commonjs --outDir dist/cjs && tsc --project tsconfig.build.json --module es6 --outDir dist/esm", + "prebuild": "rimraf dist", + "prepublishOnly": "npm run build" + }, + "dependencies": { + "@stablelib/cbor": "^1.0.1", + "adaptive-accrual-failure-detector": "^0.2.0", + "atvik": "^2.0.0", + "debug": "^4.3.1", + "fast-sha256": "^1.3.0", + "noise-secret-stream": "^3.0.2" + } +} diff --git a/packages/core/src/transport/AbstractPeer.ts b/packages/transport/src/AbstractPeer.ts similarity index 95% rename from packages/core/src/transport/AbstractPeer.ts rename to packages/transport/src/AbstractPeer.ts index 6668e64..621157b 100644 --- a/packages/core/src/transport/AbstractPeer.ts +++ b/packages/transport/src/AbstractPeer.ts @@ -10,11 +10,9 @@ import { AuthServerReply, AuthClientReplyType, AuthClientReply -} from '../auth'; -import { noId, encodeId } from '../id'; -import { WithNetwork } from '../WithNetwork'; - +} from './auth'; import { DisconnectReason } from './DisconnectReason'; +import { noId, encodeId } from './ids'; import { PeerMessageType, PeerMessage, @@ -24,6 +22,7 @@ import { AuthDataMessage } from './messages'; import { Peer } from './Peer'; +import { TransportOptions } from './TransportOptions'; /** * The interval at which pings are sent. @@ -58,7 +57,7 @@ const enum State { * protocol versions and requested features. */ export abstract class AbstractPeer implements Peer { - protected readonly parent: WithNetwork; + protected readonly transportOptions: TransportOptions; protected debug: debug.Debugger; private failureDetector: FailureDetector; @@ -88,17 +87,17 @@ export abstract class AbstractPeer implements Peer { /** * Create a new peer over the given transport. * - * @param parent - + * @param transportOptions - * the transport this belongs to * @param authProviders - * `AuthProvider` instances to use to authenticate with the other side */ public constructor( - parent: WithNetwork, + transportOptions: TransportOptions, authProviders: ReadonlyArray ) { - this.parent = parent; - this.debug = debug(parent.debugNamespace + ':peer:pending'); + this.transportOptions = transportOptions; + this.debug = debug(transportOptions.debugNamespace + ':peer:pending'); this.authProviders = authProviders; @@ -232,13 +231,13 @@ export abstract class AbstractPeer implements Peer { * to the client and this peer will start waiting for a reply. */ protected negotiateAsServer() { - this.debug = debug(this.parent.debugNamespace + ':peer:pending:server'); + this.debug = debug(this.transportOptions.debugNamespace + ':peer:pending:server'); this.state = State.WaitingForSelect; // Write the hello message const message: HelloMessage = { - id: this.parent.networkIdBinary, + id: this.transportOptions.networkId, capabilities: new Set() }; @@ -253,7 +252,7 @@ export abstract class AbstractPeer implements Peer { * client mode and wait for the initial Hello from the server. */ protected negotiateAsClient() { - this.debug = debug(this.parent.debugNamespace + ':peer:pending:client'); + this.debug = debug(this.transportOptions.debugNamespace + ':peer:pending:client'); this.registerLatencySend(); @@ -357,7 +356,7 @@ export abstract class AbstractPeer implements Peer { this.id = message.id; // Update debugging with the identifier of the peer - this.debug = debug(this.parent.debugNamespace + ':peer:' + encodeId(this.id) + ':client'); + this.debug = debug(this.transportOptions.debugNamespace + ':peer:' + encodeId(this.id) + ':client'); // TODO: Select capabilities wanted const capabilities = new Set(); @@ -367,7 +366,7 @@ export abstract class AbstractPeer implements Peer { // Send our reply const reply: SelectMessage = { - id: this.parent.networkIdBinary, + id: this.transportOptions.networkId, capabilities: capabilities }; @@ -392,7 +391,7 @@ export abstract class AbstractPeer implements Peer { this.id = message.id; // Update debugging with the identifier of the peer - this.debug = debug(this.parent.debugNamespace + ':peer:' + encodeId(this.id) + ':server'); + this.debug = debug(this.transportOptions.debugNamespace + ':peer:' + encodeId(this.id) + ':server'); // TODO: Handle incoming capabilities @@ -701,7 +700,7 @@ export abstract class AbstractPeer implements Peer { */ protected forceConnect(id: ArrayBuffer) { this.id = id; - this.debug = debug(this.parent.debugNamespace + ':peer:' + encodeId(this.id) + ':client'); + this.debug = debug(this.transportOptions.debugNamespace + ':peer:' + encodeId(this.id) + ':client'); this.latencyValues.push(0); this.switchToActive(); diff --git a/packages/core/src/transport/AbstractTransport.ts b/packages/transport/src/AbstractTransport.ts similarity index 83% rename from packages/core/src/transport/AbstractTransport.ts rename to packages/transport/src/AbstractTransport.ts index 8b15080..05658af 100644 --- a/packages/core/src/transport/AbstractTransport.ts +++ b/packages/transport/src/AbstractTransport.ts @@ -1,9 +1,7 @@ import { Event } from 'atvik'; import debug from 'debug'; -import { encodeId } from '../id'; -import { WithNetwork } from '../WithNetwork'; - +import { encodeId } from './ids'; import { Peer } from './Peer'; import { Transport } from './Transport'; import { TransportOptions } from './TransportOptions'; @@ -21,9 +19,10 @@ export class AbstractTransport implements Transport { private _started: boolean; - private _network?: WithNetwork; protected readonly peers: Set; + private _transportOptions?: TransportOptions; + /** * Create a new instance. * @@ -41,6 +40,14 @@ export class AbstractTransport implements Transport { this.debug = debug('ataraxia:no-network:' + name); } + public get transportOptions() { + if(! this._transportOptions) { + throw new Error(); + } + + return this._transportOptions; + } + /** * Event for when a new peer is connected via this transport. * @@ -61,21 +68,6 @@ export class AbstractTransport implements Transport { return this._started; } - /** - * Get the network of this transport. Can only be used after this transport - * has been started. - * - * @returns - * network instance - */ - protected get network(): WithNetwork { - if(! this._network) { - throw new Error('Can\'t access network before start() is called'); - } - - return this._network; - } - /** * Start this transport. * @@ -91,15 +83,10 @@ export class AbstractTransport implements Transport { this.debug = debug('ataraxia:' + options.networkName + ':' + this.transportName); this._started = true; + this._transportOptions = options; this.debug('Starting with id ' + encodeId(options.networkId)); - this._network = { - networkIdBinary: options.networkId, - networkId: encodeId(options.networkId), - debugNamespace: this.debug.namespace - }; - return true; } diff --git a/packages/core/src/BackOff.ts b/packages/transport/src/BackOff.ts similarity index 100% rename from packages/core/src/BackOff.ts rename to packages/transport/src/BackOff.ts diff --git a/packages/core/src/transport/DisconnectReason.ts b/packages/transport/src/DisconnectReason.ts similarity index 100% rename from packages/core/src/transport/DisconnectReason.ts rename to packages/transport/src/DisconnectReason.ts diff --git a/packages/core/src/transport/Peer.ts b/packages/transport/src/Peer.ts similarity index 100% rename from packages/core/src/transport/Peer.ts rename to packages/transport/src/Peer.ts diff --git a/packages/core/src/transport/Transport.ts b/packages/transport/src/Transport.ts similarity index 100% rename from packages/core/src/transport/Transport.ts rename to packages/transport/src/Transport.ts diff --git a/packages/core/src/transport/TransportOptions.ts b/packages/transport/src/TransportOptions.ts similarity index 100% rename from packages/core/src/transport/TransportOptions.ts rename to packages/transport/src/TransportOptions.ts diff --git a/packages/core/src/auth/AnonymousAuth.ts b/packages/transport/src/auth/AnonymousAuth.ts similarity index 100% rename from packages/core/src/auth/AnonymousAuth.ts rename to packages/transport/src/auth/AnonymousAuth.ts diff --git a/packages/core/src/auth/AuthClientFlow.ts b/packages/transport/src/auth/AuthClientFlow.ts similarity index 100% rename from packages/core/src/auth/AuthClientFlow.ts rename to packages/transport/src/auth/AuthClientFlow.ts diff --git a/packages/core/src/auth/AuthContext.ts b/packages/transport/src/auth/AuthContext.ts similarity index 100% rename from packages/core/src/auth/AuthContext.ts rename to packages/transport/src/auth/AuthContext.ts diff --git a/packages/core/src/auth/AuthProvider.ts b/packages/transport/src/auth/AuthProvider.ts similarity index 100% rename from packages/core/src/auth/AuthProvider.ts rename to packages/transport/src/auth/AuthProvider.ts diff --git a/packages/core/src/auth/AuthServerFlow.ts b/packages/transport/src/auth/AuthServerFlow.ts similarity index 100% rename from packages/core/src/auth/AuthServerFlow.ts rename to packages/transport/src/auth/AuthServerFlow.ts diff --git a/packages/core/src/auth/SharedSecretAuth.ts b/packages/transport/src/auth/SharedSecretAuth.ts similarity index 100% rename from packages/core/src/auth/SharedSecretAuth.ts rename to packages/transport/src/auth/SharedSecretAuth.ts diff --git a/packages/core/src/auth/index.ts b/packages/transport/src/auth/index.ts similarity index 100% rename from packages/core/src/auth/index.ts rename to packages/transport/src/auth/index.ts diff --git a/packages/core/src/transport/binary/decodeBinaryPeerMessage.ts b/packages/transport/src/binary/decodeBinaryPeerMessage.ts similarity index 100% rename from packages/core/src/transport/binary/decodeBinaryPeerMessage.ts rename to packages/transport/src/binary/decodeBinaryPeerMessage.ts diff --git a/packages/core/src/transport/binary/encodeBinaryPeerMessage.ts b/packages/transport/src/binary/encodeBinaryPeerMessage.ts similarity index 100% rename from packages/core/src/transport/binary/encodeBinaryPeerMessage.ts rename to packages/transport/src/binary/encodeBinaryPeerMessage.ts diff --git a/packages/core/src/transport/binary/index.ts b/packages/transport/src/binary/index.ts similarity index 72% rename from packages/core/src/transport/binary/index.ts rename to packages/transport/src/binary/index.ts index 3a9365a..d0506a0 100644 --- a/packages/core/src/transport/binary/index.ts +++ b/packages/transport/src/binary/index.ts @@ -1,3 +1,2 @@ export * from './decodeBinaryPeerMessage'; export * from './encodeBinaryPeerMessage'; -export * from './binaryStreams'; diff --git a/packages/core/src/transport/binary/tags.ts b/packages/transport/src/binary/tags.ts similarity index 100% rename from packages/core/src/transport/binary/tags.ts rename to packages/transport/src/binary/tags.ts diff --git a/packages/core/src/id/ids.ts b/packages/transport/src/ids.ts similarity index 98% rename from packages/core/src/id/ids.ts rename to packages/transport/src/ids.ts index 371a70e..593d2f7 100644 --- a/packages/core/src/id/ids.ts +++ b/packages/transport/src/ids.ts @@ -1,4 +1,4 @@ -import { randomBytes } from '../randomBytes'; +import { randomBytes } from './randomBytes'; /* * Simple encoding to custom characters. diff --git a/packages/core/src/transport/index.ts b/packages/transport/src/index.ts similarity index 78% rename from packages/core/src/transport/index.ts rename to packages/transport/src/index.ts index d8f7260..4d016dc 100644 --- a/packages/core/src/transport/index.ts +++ b/packages/transport/src/index.ts @@ -2,8 +2,12 @@ /** * Classes and types related to implementing transports. * - * @module ataraxia/transport + * @module ataraxia-transport */ +export * from './auth'; +export * from './ids'; + +export * from './BackOff'; export * from './Transport'; export * from './AbstractTransport'; @@ -16,5 +20,3 @@ export * from './DisconnectReason'; export * from './Peer'; export * from './AbstractPeer'; -export * from './StreamingPeer'; -export * from './EncryptedStreamingPeer'; diff --git a/packages/core/src/transport/messages/AuthDataMessage.ts b/packages/transport/src/messages/AuthDataMessage.ts similarity index 100% rename from packages/core/src/transport/messages/AuthDataMessage.ts rename to packages/transport/src/messages/AuthDataMessage.ts diff --git a/packages/core/src/transport/messages/AuthMessage.ts b/packages/transport/src/messages/AuthMessage.ts similarity index 100% rename from packages/core/src/transport/messages/AuthMessage.ts rename to packages/transport/src/messages/AuthMessage.ts diff --git a/packages/core/src/transport/messages/DataMessage.ts b/packages/transport/src/messages/DataMessage.ts similarity index 100% rename from packages/core/src/transport/messages/DataMessage.ts rename to packages/transport/src/messages/DataMessage.ts diff --git a/packages/core/src/transport/messages/HelloMessage.ts b/packages/transport/src/messages/HelloMessage.ts similarity index 100% rename from packages/core/src/transport/messages/HelloMessage.ts rename to packages/transport/src/messages/HelloMessage.ts diff --git a/packages/core/src/transport/messages/NodeDetailsMessage.ts b/packages/transport/src/messages/NodeDetailsMessage.ts similarity index 100% rename from packages/core/src/transport/messages/NodeDetailsMessage.ts rename to packages/transport/src/messages/NodeDetailsMessage.ts diff --git a/packages/core/src/transport/messages/NodeRequestMessage.ts b/packages/transport/src/messages/NodeRequestMessage.ts similarity index 100% rename from packages/core/src/transport/messages/NodeRequestMessage.ts rename to packages/transport/src/messages/NodeRequestMessage.ts diff --git a/packages/core/src/transport/messages/NodeSummaryMessage.ts b/packages/transport/src/messages/NodeSummaryMessage.ts similarity index 100% rename from packages/core/src/transport/messages/NodeSummaryMessage.ts rename to packages/transport/src/messages/NodeSummaryMessage.ts diff --git a/packages/core/src/transport/messages/PeerMessage.ts b/packages/transport/src/messages/PeerMessage.ts similarity index 100% rename from packages/core/src/transport/messages/PeerMessage.ts rename to packages/transport/src/messages/PeerMessage.ts diff --git a/packages/core/src/transport/messages/PeerMessageType.ts b/packages/transport/src/messages/PeerMessageType.ts similarity index 100% rename from packages/core/src/transport/messages/PeerMessageType.ts rename to packages/transport/src/messages/PeerMessageType.ts diff --git a/packages/core/src/transport/messages/SelectMessage.ts b/packages/transport/src/messages/SelectMessage.ts similarity index 100% rename from packages/core/src/transport/messages/SelectMessage.ts rename to packages/transport/src/messages/SelectMessage.ts diff --git a/packages/core/src/transport/messages/index.ts b/packages/transport/src/messages/index.ts similarity index 100% rename from packages/core/src/transport/messages/index.ts rename to packages/transport/src/messages/index.ts diff --git a/packages/core/src/randomBytes.ts b/packages/transport/src/randomBytes.ts similarity index 100% rename from packages/core/src/randomBytes.ts rename to packages/transport/src/randomBytes.ts diff --git a/packages/core/test/transport/AbstractPeer.ts b/packages/transport/test/AbstractPeer.test.ts similarity index 68% rename from packages/core/test/transport/AbstractPeer.ts rename to packages/transport/test/AbstractPeer.test.ts index 45db3a1..b9b3ad3 100644 --- a/packages/core/test/transport/AbstractPeer.ts +++ b/packages/transport/test/AbstractPeer.test.ts @@ -1,12 +1,17 @@ -import { AnonymousAuth } from '../../src/auth'; -import { encodeId, generateId } from '../../src/id'; -import { AbstractPeer, PeerMessageType, PeerMessage, DisconnectReason } from '../../src/transport'; -import { WithNetwork } from '../../src/WithNetwork'; +import { + AbstractPeer, + PeerMessageType, + PeerMessage, + DisconnectReason, + AnonymousAuth, + TransportOptions, + generateId +} from '../src'; describe('Transport: AbstractPeer', function() { it('Server and client can negotiate', done => { - const server = new TestPeer(testNetwork(), [ AnonymousAuth.INSTANCE ]); - const client = new TestPeer(testNetwork(), [ AnonymousAuth.INSTANCE ]); + const server = new TestPeer(testOptions(), [ AnonymousAuth.INSTANCE ]); + const client = new TestPeer(testOptions(), [ AnonymousAuth.INSTANCE ]); client.other = server; server.other = client; @@ -29,11 +34,12 @@ describe('Transport: AbstractPeer', function() { * @returns - * fake network */ -function testNetwork(): WithNetwork { +function testOptions(): TransportOptions { const id = generateId(); return { - networkId: encodeId(id), - networkIdBinary: id, + endpoint: false, + networkId: id, + networkName: 'test', debugNamespace: 'tests:abstract-peer' }; } diff --git a/packages/transport/tsconfig.build.json b/packages/transport/tsconfig.build.json new file mode 100644 index 0000000..ef83010 --- /dev/null +++ b/packages/transport/tsconfig.build.json @@ -0,0 +1,13 @@ + +{ + "extends": "../../tsconfig.settings.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": "./src", + "outDir": "dist/cjs", + "declarationDir": "dist/types", + }, + "include": [ + "src" + ] +} diff --git a/packages/ws-client/package.json b/packages/ws-client/package.json index 49e9d21..85b45dc 100644 --- a/packages/ws-client/package.json +++ b/packages/ws-client/package.json @@ -17,7 +17,7 @@ "prepublishOnly": "npm run build" }, "dependencies": { - "ataraxia": "^0.10.0", + "ataraxia-transport": "^0.10.0", "debug": "^4.3.1" } } diff --git a/packages/ws-client/src/AbstractWebSocketPeer.ts b/packages/ws-client/src/AbstractWebSocketPeer.ts index f988696..6297fc0 100644 --- a/packages/ws-client/src/AbstractWebSocketPeer.ts +++ b/packages/ws-client/src/AbstractWebSocketPeer.ts @@ -1,12 +1,13 @@ -import { AuthProvider, WithNetwork } from 'ataraxia'; import { + AuthProvider, AbstractPeer, PeerMessageType, PeerMessage, encodeBinaryPeerMessage, decodeBinaryPeerMessage, - DisconnectReason -} from 'ataraxia/transport'; + DisconnectReason, + TransportOptions +} from 'ataraxia-transport'; import { WebSocket } from './WebSocket'; @@ -17,8 +18,11 @@ import { WebSocket } from './WebSocket'; export abstract class AbstractWebSocketPeer extends AbstractPeer { private socket?: WebSocket; - public constructor(parent: WithNetwork, authProviders: ReadonlyArray) { - super(parent, authProviders); + public constructor( + transportOptions: TransportOptions, + authProviders: ReadonlyArray + ) { + super(transportOptions, authProviders); } public setSocket(socket: WebSocket) { diff --git a/packages/ws-client/src/WebSocketClientPeer.ts b/packages/ws-client/src/WebSocketClientPeer.ts index c8bec61..e5b4250 100644 --- a/packages/ws-client/src/WebSocketClientPeer.ts +++ b/packages/ws-client/src/WebSocketClientPeer.ts @@ -1,5 +1,9 @@ -import { AuthProvider, BackOff, WithNetwork } from 'ataraxia'; -import { DisconnectReason } from 'ataraxia/transport'; +import { + AuthProvider, + BackOff, + DisconnectReason, + TransportOptions +} from 'ataraxia-transport'; import { AbstractWebSocketPeer } from './AbstractWebSocketPeer'; import { WebSocketFactory } from './WebSocketFactory'; @@ -13,12 +17,12 @@ export class WebSocketClientPeer extends AbstractWebSocketPeer { private connectTimeout: any; public constructor( - parent: WithNetwork, + transportOptions: TransportOptions, authProviders: ReadonlyArray, factory: WebSocketFactory, url: string ) { - super(parent, authProviders); + super(transportOptions, authProviders); this.factory = factory; this.url = url; diff --git a/packages/ws-client/src/WebSocketClientTransport.ts b/packages/ws-client/src/WebSocketClientTransport.ts index 55912c7..ac7d151 100644 --- a/packages/ws-client/src/WebSocketClientTransport.ts +++ b/packages/ws-client/src/WebSocketClientTransport.ts @@ -1,5 +1,8 @@ -import { AuthProvider } from 'ataraxia'; -import { AbstractTransport, TransportOptions } from 'ataraxia/transport'; +import { + AuthProvider, + AbstractTransport, + TransportOptions +} from 'ataraxia-transport'; import { WebSocketClientPeer } from './WebSocketClientPeer'; import { WebSocketFactory } from './WebSocketFactory'; @@ -80,7 +83,7 @@ export class WebSocketClientTransport extends AbstractTransport { || defaultWebSocketFactory; this.peer = new WebSocketClientPeer( - this.network, + this.transportOptions, this.options.authentication, factory, this.options.url diff --git a/packages/ws-server/package.json b/packages/ws-server/package.json index 2c1bd74..81296ea 100644 --- a/packages/ws-server/package.json +++ b/packages/ws-server/package.json @@ -15,7 +15,7 @@ "prepublishOnly": "npm run build" }, "dependencies": { - "ataraxia": "^0.10.0", + "ataraxia-transport": "^0.10.0", "ataraxia-ws-client": "^0.10.0", "debug": "^4.3.1", "ws": "^7.4.6" diff --git a/packages/ws-server/src/WebSocketServerTransport.ts b/packages/ws-server/src/WebSocketServerTransport.ts index add1898..38e6eac 100644 --- a/packages/ws-server/src/WebSocketServerTransport.ts +++ b/packages/ws-server/src/WebSocketServerTransport.ts @@ -1,8 +1,11 @@ import WS from 'ws'; -import { AuthProvider } from 'ataraxia'; +import { + AbstractTransport, + AuthProvider, + TransportOptions +} from 'ataraxia-transport'; import { AbstractWebSocketPeer } from 'ataraxia-ws-client'; -import { AbstractTransport, TransportOptions } from 'ataraxia/transport'; export interface WebSocketServerTransportOptions extends WS.ServerOptions { /** @@ -33,7 +36,7 @@ export class WebSocketServerTransport extends AbstractTransport { this.server = new WS.Server(this.options); this.server.on('connection', socket => { - const peer = new WebsocketServerPeer(this.network, this.options.authentication); + const peer = new WebsocketServerPeer(this.transportOptions, this.options.authentication); this.addPeer(peer); peer.setSocket(socket); diff --git a/tsconfig.json b/tsconfig.json index 87599bc..65285da 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,6 @@ "baseUrl": ".", "paths": { "ataraxia": [ "./packages/core/src" ], - "ataraxia/transport": [ "./packages/core/src/transport" ], "ataraxia-*": [ "./packages/*/src" ] } }, @@ -18,7 +17,8 @@ "name": "Ataraxia", "entryPoints": [ "packages/core/src/index.ts", - "packages/core/src/transport/index.ts", + "packages/transport/src/index.ts", + "packages/transport-streams/src/index.ts", "packages/local/src/index.ts", "packages/tcp/src/index.ts", "packages/hyperswarm/src/index.ts",