Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST_DATABASE_URL=postgres://teletype:password@localhost:5433/teletype-server-test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.env
.DS_Store
test-database
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ To run teletype-client tests locally, you'll first need to have:
npm test
```

3. Create postgresql docker instance:

```
docker-compose up -d
cp .env.local.example .env
```

## TODO

* [ ] Document APIs
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3'

services:
test-database:
image: postgres
ports:
- 5433:5432
volumes:
- ./test-database:/var/lib/postgresql/data
restart: always
environment:
POSTGRES_USER: teletype
POSTGRES_PASSWORD: password
POSTGRES_DB: teletype-server-test
networks:
teletype:

networks:
teletype:
30 changes: 30 additions & 0 deletions lib/socketcluster-pub-sub-gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const socketCluster = require('socketcluster-client');
const {Disposable} = require('event-kit')
const Errors = require('./errors')

module.exports =
class SocketClusterPubSubGateway {
constructor ({}) {
this.socketClusterClient = createDisconnectedSocketClusterClient()
}

async subscribe (channelName, eventName, callback) {
channelName = channelName.replace(/\//g, '.')
let eventChannel = this.socketClusterClient.subscribe(`${channelName}.${eventName}`);

eventChannel.watch(callback);

return new Disposable(() => {
this.socketClusterClient.unsubscribe(`${channelName}.${eventName}`)
})
}
}

function createDisconnectedSocketClusterClient () {
const options = {
port: 8000
};

const socket = socketCluster.create(options);
return socket
}
17 changes: 15 additions & 2 deletions lib/teletype-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PeerPool = require('./peer-pool')
const Portal = require('./portal')
const Errors = require('./errors')
const PusherPubSubGateway = require('./pusher-pub-sub-gateway')
const SocketClusterPubSubGateway = require('./socketcluster-pub-sub-gateway')
const RestGateway = require('./rest-gateway')
const {Emitter} = require('event-kit')
const NOOP = () => {}
Expand All @@ -13,9 +14,21 @@ const LOCAL_PROTOCOL_VERSION = 9

module.exports =
class TeletypeClient {
constructor ({restGateway, pubSubGateway, connectionTimeout, tetherDisconnectWindow, testEpoch, pusherKey, pusherOptions, baseURL, didCreateOrJoinPortal}) {
constructor ({restGateway, pubSubGateway, connectionTimeout, tetherDisconnectWindow, testEpoch, activePubSubGateway, pusherKey, pusherOptions, baseURL, didCreateOrJoinPortal}) {
function getActivePubSubGateway(activePubSubGateway) {
switch(activePubSubGateway) {
case 'socketcluster':
return new SocketClusterPubSubGateway({})
break;

case 'pusher':
default:
return new PusherPubSubGateway({key: pusherKey, options: pusherOptions})
break;
}
}
this.restGateway = restGateway || new RestGateway({baseURL})
this.pubSubGateway = pubSubGateway || new PusherPubSubGateway({key: pusherKey, options: pusherOptions})
this.pubSubGateway = pubSubGateway || getActivePubSubGateway(activePubSubGateway)
this.connectionTimeout = connectionTimeout || 5000
this.tetherDisconnectWindow = tetherDisconnectWindow || DEFAULT_TETHER_DISCONNECT_WINDOW
this.testEpoch = testEpoch
Expand Down
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"event-kit": "^2.3.0",
"google-protobuf": "^3.5.0",
"pusher-js": "^4.2.2",
"socketcluster-client": "^13.0.0",
"uuid": "^3.2.1",
"webrtc-adapter": "~6.1"
},
Expand Down