Skip to content

Commit 62e3171

Browse files
authored
Merge pull request #39 from datapartyjs/ws-host
websocket host
2 parents e6d0b9f + 918affb commit 62e3171

30 files changed

Lines changed: 685 additions & 63 deletions

examples/party/schema/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class User extends ISchema {
1414
photo: { type: String, maxlength: 500, description: 'user photo url' },
1515
created: Utils.created,
1616
enabled: Boolean,
17-
profile: Utils.profile,
17+
profile: Object,
1818
tutorial: {
1919
done: Boolean
2020
}

examples/test-peer-party.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ async function main(){
1818
comms: new Dataparty.Comms.RTCSocketComms({
1919
host: true,
2020
wrtc: WRTC,
21-
trickle: true
21+
trickle: true,
22+
discoverRemoteIdentity: true
2223
}),
2324
hostParty: hostLocal,
2425
model: BouncerModel,
@@ -29,7 +30,8 @@ async function main(){
2930
let peer2 = new Dataparty.PeerParty({
3031
comms: new Dataparty.Comms.RTCSocketComms({
3132
wrtc: WRTC,
32-
trickle: true
33+
trickle: true,
34+
session: 'foobar'
3335
}),
3436
model: BouncerModel,
3537
config: new Dataparty.Config.MemoryConfig()
@@ -40,7 +42,7 @@ async function main(){
4042
await peer1.loadIdentity()
4143
await peer2.loadIdentity()
4244

43-
peer1.comms.remoteIdentity = peer2.identity
45+
//peer1.comms.remoteIdentity = peer2.identity
4446
peer2.comms.remoteIdentity = peer1.identity
4547

4648
await peer1.start()

examples/test-service-host.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ class ExampleService extends Dataparty.IService {
2525

2626
async function main(){
2727

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

31-
let party = new Dataparty.MongoParty({
32-
uri,
31+
const path = '/data/datparty/srv-party'
32+
33+
let party = new Dataparty.TingoParty({
34+
path,
3335
model: BouncerClientModels,
3436
serverModels: BouncerServerModels,
3537
config: new Dataparty.Config.MemoryConfig()
3638
})
3739

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

40-
const build = await service.compile(Path.join(__dirname,'../dataparty'), true)
43+
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
4144

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

@@ -46,7 +49,11 @@ async function main(){
4649
sendFullErrors: true
4750
})
4851

49-
const host = new Dataparty.ServiceHost({runner, trust_proxy: true})
52+
const host = new Dataparty.ServiceHost({
53+
runner,
54+
trust_proxy: true,
55+
wsEnabled: true
56+
})
5057

5158
await party.start()
5259
await runner.start()

examples/test-service-node-host.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const Path = require('path')
2+
const debug = require('debug')('test.server-db')
3+
const Dataparty = require('../src')
4+
5+
const BouncerServerModels = require('@dataparty/bouncer-model')
6+
const BouncerClientModels = require('@dataparty/bouncer-model/dist/bouncer-model.json')
7+
8+
class ExampleService extends Dataparty.IService {
9+
constructor(opts){
10+
super(opts)
11+
12+
this.addMiddleware(Dataparty.middleware_paths.pre.decrypt)
13+
this.addMiddleware(Dataparty.middleware_paths.pre.validate)
14+
15+
this.addMiddleware(Dataparty.middleware_paths.post.validate)
16+
this.addMiddleware(Dataparty.middleware_paths.post.encrypt)
17+
18+
this.addEndpoint(Dataparty.endpoint_paths.echo)
19+
this.addEndpoint(Dataparty.endpoint_paths.secureecho)
20+
this.addEndpoint(Dataparty.endpoint_paths.identity)
21+
this.addEndpoint(Dataparty.endpoint_paths.version)
22+
}
23+
24+
}
25+
26+
async function main(){
27+
28+
29+
//const uri = 'mongodb://localhost:27017/server-party-test'
30+
//debug('db location', uri)
31+
32+
const path = '/data/datparty/srv-party'
33+
34+
let party = new Dataparty.TingoParty({
35+
path,
36+
model: BouncerClientModels,
37+
serverModels: BouncerServerModels,
38+
config: new Dataparty.Config.JsonFileConfig({basePath: '/data/datparty/'})
39+
})
40+
41+
const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })
42+
43+
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
44+
45+
debug('built', Object.keys(build))
46+
47+
const runner = new Dataparty.ServiceRunnerNode({
48+
party, service,
49+
sendFullErrors: false
50+
})
51+
52+
const host = new Dataparty.ServiceHost({
53+
runner,
54+
trust_proxy: true,
55+
wsEnabled: true
56+
})
57+
58+
await party.start()
59+
await runner.start()
60+
await host.start()
61+
62+
console.log('started')
63+
64+
//process.exit()
65+
}
66+
67+
68+
69+
main().catch(err=>{
70+
console.error(err)
71+
})

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.2.14",
4+
"version": "1.2.15",
55
"main": "dist/dataparty.js",
66
"frontend": "dist/dataparty-browser.js",
77
"backend": "dist/dataparty.js",
@@ -17,8 +17,7 @@
1717
"context": "browser",
1818
"source": "src/index-browser.js",
1919
"optimize": false,
20-
"includeNodeModules": true,
21-
"scopeHoist": false,
20+
"outputFormat": "global",
2221
"engines": {
2322
"browsers": "Chrome 80"
2423
}
@@ -84,6 +83,7 @@
8483
"mkdirp": "^0.5.1",
8584
"moment": "^2.29.4",
8685
"morgan": "^1.10.0",
86+
"multer": "^1.4.5-lts.1",
8787
"nconf": "^0.10.0",
8888
"node-persist": "^3.0.1",
8989
"origin-router": "^1.6.4",
@@ -102,6 +102,7 @@
102102
"uuidv4": "^6.2.12",
103103
"vm2": "^3.9.2",
104104
"websocket": "github:sevenbitbyte/WebSocket-Node#parcel-build",
105+
"ws": "^8.11.0",
105106
"zangodb": "github:sevenbitbyte/zangodb#hash-patch"
106107
},
107108
"devDependencies": {

src/bouncer/db/loki-db.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ module.exports = class LokiDb extends IDb {
2424
this.error = null
2525
}
2626

27+
static get LokiLocalStorageAdapter(){
28+
return Loki.LokiLocalStorageAdapter
29+
}
30+
2731

2832
async start(){
2933

src/comms/peer-comms.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const SocketComms = require('./socket-comms')
77
const AUTH_TIMEOUT_MS = 3000
88

99
class PeerComms extends SocketComms {
10-
constructor({remoteIdentity, host, party, socket}){
11-
super({remoteIdentity, party})
10+
constructor({remoteIdentity, discoverRemoteIdentity, host, party, socket, ...options}){
11+
super({remoteIdentity, discoverRemoteIdentity, party, ...options})
1212

1313
this.socket = socket || null
1414

15-
this.host = host
15+
this.host = host //! Is comms host
1616
this.party = party
1717
this.oncall = null
1818

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

116116
if(this.host){
117+
debug('host mode comms')
117118
this.socket.on('connect', this.handleClientConnection.bind(this))
118119
this.socket.on('data', this.handleClientCall.bind(this))
119120
}
120121
else{
122+
debug('client mode comms')
121123
this.socket.on('connect', this.onopen.bind(this))
122124
this.socket.on('data', this.handleMessage.bind(this))
123125
}
@@ -132,7 +134,7 @@ class PeerComms extends SocketComms {
132134
}
133135

134136
close(){
135-
debug('Client closing connection')
137+
debug('closing connection')
136138
this.socket.destroy()
137139
}
138140

src/comms/rest-comms.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const debug = require('debug')('dataparty.comms.rest')
44

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

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

1010

@@ -132,8 +132,8 @@ class RestComms extends EventEmitter {
132132

133133
let reply
134134
try {
135-
const str = await RestComms.HttpPost(fullPath, content)
136-
reply = JSON.parse(str)
135+
reply = await RestComms.HttpPost(fullPath, content)
136+
//reply = JSON.parse(str)
137137

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

206-
this.remoteIdentity = dataparty_crypto.Identity.fromString(serverIdentity)
206+
this.remoteIdentity = new dataparty_crypto.Identity(serverIdentity)
207207
}
208208

209209
return this.remoteIdentity
@@ -325,6 +325,7 @@ class RestComms extends EventEmitter {
325325
}
326326
}
327327

328+
/*
328329
async websocket(reuse = true) {
329330
if (reuse && this.websocketComm && this.websocketComm.connected) {
330331
return this.websocketComm
@@ -349,7 +350,7 @@ class RestComms extends EventEmitter {
349350
350351
return comm.authorized()
351352
})
352-
}
353+
}*/
353354

354355
static async HttpRequest(verb, url, data) {
355356

src/comms/rtc-socket-comms.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ const debug = require('debug')('dataparty.comms.rtcsocketcomms')
33
const SimplePeer = require('simple-peer')
44
const PeerComms = require('./peer-comms')
55

6-
const AUTH_TIMEOUT_MS = 3000
76

87
class RTCSocketComms extends PeerComms {
9-
constructor({remoteIdentity, host, party, wrtc, trickle = false}){
10-
super({remoteIdentity, host, party})
8+
constructor({remoteIdentity, host, party, wrtc, trickle = false, ...options}){
9+
super({remoteIdentity, host, party, ...options})
1110

1211
this.rtcSettings = {
1312
wrtc,

src/comms/socket-comms.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ const RosShim = require('./ros-shim')
1010

1111

1212
class SocketComms extends EventEmitter {
13-
constructor({session, uri, party, remoteIdentity}){
13+
constructor({session, uri, party, remoteIdentity, discoverRemoteIdentity}){
1414
super()
1515
this.uri = uri
1616
this.session = session
1717
this.remoteIdentity = remoteIdentity
18+
this.discoverRemoteIdentity = discoverRemoteIdentity
1819

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

@@ -84,10 +85,20 @@ class SocketComms extends EventEmitter {
8485

8586
return resolve(msg.decrypt(this.party._identity).then(content=>{
8687
const senderPub = Routines.extractPublicKeys(msg.enc)
88+
debug('sender', sender, '\tdiscover', this.discoverRemoteIdentity)
89+
if(this.discoverRemoteIdentity && !sender){
90+
debug('discovered remote identity', senderPub)
91+
this.remoteIdentity = {
92+
key: {
93+
public: senderPub
94+
}
95+
}
96+
sender = this.remoteIdentity
97+
}
8798
debug(`senderPub - ${senderPub}`)
8899

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

93104
debug('decrypted data')

0 commit comments

Comments
 (0)