Skip to content

Commit

Permalink
Merge pull request #39 from datapartyjs/ws-host
Browse files Browse the repository at this point in the history
websocket host
  • Loading branch information
sevenbitbyte committed Jan 8, 2023
2 parents e6d0b9f + 918affb commit 62e3171
Show file tree
Hide file tree
Showing 30 changed files with 685 additions and 63 deletions.
2 changes: 1 addition & 1 deletion examples/party/schema/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class User extends ISchema {
photo: { type: String, maxlength: 500, description: 'user photo url' },
created: Utils.created,
enabled: Boolean,
profile: Utils.profile,
profile: Object,
tutorial: {
done: Boolean
}
Expand Down
8 changes: 5 additions & 3 deletions examples/test-peer-party.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ async function main(){
comms: new Dataparty.Comms.RTCSocketComms({
host: true,
wrtc: WRTC,
trickle: true
trickle: true,
discoverRemoteIdentity: true
}),
hostParty: hostLocal,
model: BouncerModel,
Expand All @@ -29,7 +30,8 @@ async function main(){
let peer2 = new Dataparty.PeerParty({
comms: new Dataparty.Comms.RTCSocketComms({
wrtc: WRTC,
trickle: true
trickle: true,
session: 'foobar'
}),
model: BouncerModel,
config: new Dataparty.Config.MemoryConfig()
Expand All @@ -40,7 +42,7 @@ async function main(){
await peer1.loadIdentity()
await peer2.loadIdentity()

peer1.comms.remoteIdentity = peer2.identity
//peer1.comms.remoteIdentity = peer2.identity
peer2.comms.remoteIdentity = peer1.identity

await peer1.start()
Expand Down
19 changes: 13 additions & 6 deletions examples/test-service-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ class ExampleService extends Dataparty.IService {

async function main(){

const uri = 'mongodb://localhost:27017/server-party-test'
debug('db location', uri)
//const uri = 'mongodb://localhost:27017/server-party-test'
//debug('db location', uri)

let party = new Dataparty.MongoParty({
uri,
const path = '/data/datparty/srv-party'

let party = new Dataparty.TingoParty({
path,
model: BouncerClientModels,
serverModels: BouncerServerModels,
config: new Dataparty.Config.MemoryConfig()
})


const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })

const build = await service.compile(Path.join(__dirname,'../dataparty'), true)
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)

debug('built', Object.keys(build))

Expand All @@ -46,7 +49,11 @@ async function main(){
sendFullErrors: true
})

const host = new Dataparty.ServiceHost({runner, trust_proxy: true})
const host = new Dataparty.ServiceHost({
runner,
trust_proxy: true,
wsEnabled: true
})

await party.start()
await runner.start()
Expand Down
71 changes: 71 additions & 0 deletions examples/test-service-node-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const Path = require('path')
const debug = require('debug')('test.server-db')
const Dataparty = require('../src')

const BouncerServerModels = require('@dataparty/bouncer-model')
const BouncerClientModels = require('@dataparty/bouncer-model/dist/bouncer-model.json')

class ExampleService extends Dataparty.IService {
constructor(opts){
super(opts)

this.addMiddleware(Dataparty.middleware_paths.pre.decrypt)
this.addMiddleware(Dataparty.middleware_paths.pre.validate)

this.addMiddleware(Dataparty.middleware_paths.post.validate)
this.addMiddleware(Dataparty.middleware_paths.post.encrypt)

this.addEndpoint(Dataparty.endpoint_paths.echo)
this.addEndpoint(Dataparty.endpoint_paths.secureecho)
this.addEndpoint(Dataparty.endpoint_paths.identity)
this.addEndpoint(Dataparty.endpoint_paths.version)
}

}

async function main(){


//const uri = 'mongodb://localhost:27017/server-party-test'
//debug('db location', uri)

const path = '/data/datparty/srv-party'

let party = new Dataparty.TingoParty({
path,
model: BouncerClientModels,
serverModels: BouncerServerModels,
config: new Dataparty.Config.JsonFileConfig({basePath: '/data/datparty/'})
})

const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })

const build = await service.compile(Path.join(__dirname,'/dataparty'), true)

debug('built', Object.keys(build))

const runner = new Dataparty.ServiceRunnerNode({
party, service,
sendFullErrors: false
})

const host = new Dataparty.ServiceHost({
runner,
trust_proxy: true,
wsEnabled: true
})

await party.start()
await runner.start()
await host.start()

console.log('started')

//process.exit()
}



main().catch(err=>{
console.error(err)
})
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dataparty/api",
"private": false,
"version": "1.2.14",
"version": "1.2.15",
"main": "dist/dataparty.js",
"frontend": "dist/dataparty-browser.js",
"backend": "dist/dataparty.js",
Expand All @@ -17,8 +17,7 @@
"context": "browser",
"source": "src/index-browser.js",
"optimize": false,
"includeNodeModules": true,
"scopeHoist": false,
"outputFormat": "global",
"engines": {
"browsers": "Chrome 80"
}
Expand Down Expand Up @@ -84,6 +83,7 @@
"mkdirp": "^0.5.1",
"moment": "^2.29.4",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nconf": "^0.10.0",
"node-persist": "^3.0.1",
"origin-router": "^1.6.4",
Expand All @@ -102,6 +102,7 @@
"uuidv4": "^6.2.12",
"vm2": "^3.9.2",
"websocket": "github:sevenbitbyte/WebSocket-Node#parcel-build",
"ws": "^8.11.0",
"zangodb": "github:sevenbitbyte/zangodb#hash-patch"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/bouncer/db/loki-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module.exports = class LokiDb extends IDb {
this.error = null
}

static get LokiLocalStorageAdapter(){
return Loki.LokiLocalStorageAdapter
}


async start(){

Expand Down
10 changes: 6 additions & 4 deletions src/comms/peer-comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const SocketComms = require('./socket-comms')
const AUTH_TIMEOUT_MS = 3000

class PeerComms extends SocketComms {
constructor({remoteIdentity, host, party, socket}){
super({remoteIdentity, party})
constructor({remoteIdentity, discoverRemoteIdentity, host, party, socket, ...options}){
super({remoteIdentity, discoverRemoteIdentity, party, ...options})

this.socket = socket || null

this.host = host
this.host = host //! Is comms host
this.party = party
this.oncall = null

Expand Down Expand Up @@ -114,10 +114,12 @@ class PeerComms extends SocketComms {
this.socket.on('close', this.onclose.bind(this))

if(this.host){
debug('host mode comms')
this.socket.on('connect', this.handleClientConnection.bind(this))
this.socket.on('data', this.handleClientCall.bind(this))
}
else{
debug('client mode comms')
this.socket.on('connect', this.onopen.bind(this))
this.socket.on('data', this.handleMessage.bind(this))
}
Expand All @@ -132,7 +134,7 @@ class PeerComms extends SocketComms {
}

close(){
debug('Client closing connection')
debug('closing connection')
this.socket.destroy()
}

Expand Down
11 changes: 6 additions & 5 deletions src/comms/rest-comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const debug = require('debug')('dataparty.comms.rest')

const dataparty_crypto = require('@dataparty/crypto')

const WebsocketComms = require('./websocket-comms')
//const WebsocketComms = require('./old-websocket-comms')
const AuthError = require('../errors/auth-error')


Expand Down Expand Up @@ -132,8 +132,8 @@ class RestComms extends EventEmitter {

let reply
try {
const str = await RestComms.HttpPost(fullPath, content)
reply = JSON.parse(str)
reply = await RestComms.HttpPost(fullPath, content)
//reply = JSON.parse(str)

// debug('raw reply ->', reply)
} catch (error) {
Expand Down Expand Up @@ -203,7 +203,7 @@ class RestComms extends EventEmitter {
const serverIdentity = await RestComms.HttpGet(this.uri + `${this.uriPrefix}identity`)
debug('server identity - ', serverIdentity)

this.remoteIdentity = dataparty_crypto.Identity.fromString(serverIdentity)
this.remoteIdentity = new dataparty_crypto.Identity(serverIdentity)
}

return this.remoteIdentity
Expand Down Expand Up @@ -325,6 +325,7 @@ class RestComms extends EventEmitter {
}
}

/*
async websocket(reuse = true) {
if (reuse && this.websocketComm && this.websocketComm.connected) {
return this.websocketComm
Expand All @@ -349,7 +350,7 @@ class RestComms extends EventEmitter {
return comm.authorized()
})
}
}*/

static async HttpRequest(verb, url, data) {

Expand Down
5 changes: 2 additions & 3 deletions src/comms/rtc-socket-comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ const debug = require('debug')('dataparty.comms.rtcsocketcomms')
const SimplePeer = require('simple-peer')
const PeerComms = require('./peer-comms')

const AUTH_TIMEOUT_MS = 3000

class RTCSocketComms extends PeerComms {
constructor({remoteIdentity, host, party, wrtc, trickle = false}){
super({remoteIdentity, host, party})
constructor({remoteIdentity, host, party, wrtc, trickle = false, ...options}){
super({remoteIdentity, host, party, ...options})

this.rtcSettings = {
wrtc,
Expand Down
15 changes: 13 additions & 2 deletions src/comms/socket-comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ const RosShim = require('./ros-shim')


class SocketComms extends EventEmitter {
constructor({session, uri, party, remoteIdentity}){
constructor({session, uri, party, remoteIdentity, discoverRemoteIdentity}){
super()
this.uri = uri
this.session = session
this.remoteIdentity = remoteIdentity
this.discoverRemoteIdentity = discoverRemoteIdentity

this.party = party //used for access to primary identity

Expand Down Expand Up @@ -84,10 +85,20 @@ class SocketComms extends EventEmitter {

return resolve(msg.decrypt(this.party._identity).then(content=>{
const senderPub = Routines.extractPublicKeys(msg.enc)
debug('sender', sender, '\tdiscover', this.discoverRemoteIdentity)
if(this.discoverRemoteIdentity && !sender){
debug('discovered remote identity', senderPub)
this.remoteIdentity = {
key: {
public: senderPub
}
}
sender = this.remoteIdentity
}
debug(`senderPub - ${senderPub}`)

if(senderPub.box != sender.key.public.box || senderPub.sign != sender.key.public.sign){
return Promise.reject('TRUST - reply is not from service')
return Promise.reject('TRUST - reply is not from expected remote')
}

debug('decrypted data')
Expand Down
Loading

0 comments on commit 62e3171

Please sign in to comment.