Skip to content

Commit b40d47f

Browse files
committed
peer service
1 parent 342a6cc commit b40d47f

4 files changed

Lines changed: 200 additions & 7 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
const debug = require('debug')('test.peer-party-service')
2+
const WRTC = require('wrtc')
3+
const Path = require('path')
4+
const BouncerModel = require('@dataparty/bouncer-model/dist/bouncer-model.json')
5+
const Dataparty = require('../src')
6+
7+
const BouncerServerModels = require('@dataparty/bouncer-model')
8+
9+
class ExampleService extends Dataparty.IService {
10+
constructor(opts){
11+
super(opts)
12+
13+
this.addMiddleware(Dataparty.middleware_paths.pre.decrypt)
14+
this.addMiddleware(Dataparty.middleware_paths.pre.validate)
15+
16+
this.addMiddleware(Dataparty.middleware_paths.post.validate)
17+
this.addMiddleware(Dataparty.middleware_paths.post.encrypt)
18+
19+
this.addEndpoint(Dataparty.endpoint_paths.echo)
20+
this.addEndpoint(Dataparty.endpoint_paths.secureecho)
21+
this.addEndpoint(Dataparty.endpoint_paths.identity)
22+
this.addEndpoint(Dataparty.endpoint_paths.version)
23+
}
24+
25+
}
26+
27+
async function main(){
28+
const dbPath = '/tmp/local-peer-party-service'
29+
30+
debug('db location', dbPath)
31+
32+
let hostLocal = new Dataparty.TingoParty({
33+
path: dbPath,
34+
model: BouncerModel,
35+
serverModels: BouncerServerModels,
36+
config: new Dataparty.Config.JsonFileConfig({basePath: dbPath})
37+
})
38+
39+
const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })
40+
41+
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
42+
43+
debug('built', Object.keys(build))
44+
45+
const runner = new Dataparty.ServiceRunnerNode({
46+
party: hostLocal, service,
47+
sendFullErrors: false
48+
})
49+
50+
/*let hostLocal = new Dataparty.LokiParty({
51+
path: dbPath,
52+
model: BouncerModel,
53+
config: new Dataparty.Config.MemoryConfig()
54+
})*/
55+
56+
let peer1 = new Dataparty.PeerParty({
57+
comms: new Dataparty.Comms.RTCSocketComms({
58+
host: true,
59+
wrtc: WRTC,
60+
trickle: true,
61+
discoverRemoteIdentity: true
62+
}),
63+
hostParty: hostLocal,
64+
hostRunner: runner,
65+
model: BouncerModel,
66+
config: new Dataparty.Config.MemoryConfig()
67+
})
68+
69+
70+
let peer2 = new Dataparty.PeerParty({
71+
comms: new Dataparty.Comms.RTCSocketComms({
72+
wrtc: WRTC,
73+
trickle: true,
74+
session: 'foobar'
75+
}),
76+
model: BouncerModel,
77+
config: new Dataparty.Config.MemoryConfig()
78+
})
79+
80+
81+
82+
await peer1.loadIdentity()
83+
await peer2.loadIdentity()
84+
85+
//peer1.comms.remoteIdentity = peer2.identity
86+
peer2.comms.remoteIdentity = peer1.identity
87+
88+
await peer1.start()
89+
await runner.start()
90+
91+
await peer2.start()
92+
93+
peer1.comms.socket.on('signal', data=>{
94+
debug('p1 >> p2', data)
95+
peer2.comms.socket.signal(data)
96+
})
97+
98+
peer2.comms.socket.on('signal', data=>{
99+
debug('p1 << p2', data)
100+
peer1.comms.socket.signal(data)
101+
})
102+
103+
debug('waiting for auth')
104+
await Promise.all([
105+
peer1.comms.authorized(),
106+
peer2.comms.authorized()
107+
])
108+
109+
debug('authed')
110+
111+
const remoteVersion = await peer2.comms.call('version')
112+
const remoteId = await peer2.comms.call('identity')
113+
114+
115+
116+
let user = (await peer2.find()
117+
.type('user')
118+
.where('name').equals('tester')
119+
.exec())[0]
120+
121+
122+
if(!user){
123+
debug('creating document')
124+
user = await peer2.createDocument('user', {name: 'tester', created: (new Date()).toISOString() })
125+
}
126+
else{
127+
debug('loaded document')
128+
}
129+
130+
131+
console.log(remoteId)
132+
console.log(remoteVersion)
133+
134+
console.log(user.data)
135+
process.exit()
136+
}
137+
138+
139+
main().catch(err=>{
140+
console.error(err)
141+
})

src/comms/peer-comms.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const debug = require('debug')('dataparty.comms.peercomms')
22

3+
const HttpMocks = require('node-mocks-http')
34

45
const SocketOp = require('./op/socket-op')
56
const SocketComms = require('./socket-comms')
@@ -200,7 +201,9 @@ class PeerComms extends SocketComms {
200201

201202
debug('running peer-call endpoint =', path, data)
202203

203-
return await callOp.run()
204+
const reply = await callOp.run()
205+
206+
return reply.result
204207
}
205208

206209
async start(){
@@ -275,11 +278,58 @@ class PeerComms extends SocketComms {
275278
}
276279

277280
async handleCallOp(op){
278-
debug('peer-call')
279-
if(op.input.endpoint == 'api-v2-peer-bouncer'){
281+
debug('peer-call', op.input.endpoint)
282+
283+
if(this.party.hostRunner){
284+
285+
debug('calling runner')
286+
287+
if(op.input.endpoint == 'api-v2-peer-bouncer'){
288+
debug('ask->',op.input.data)
289+
op.result = {result: await this.party.handleCall(op.input.data) }
290+
291+
op.setState(HostOp.STATES.Finished_Success)
292+
return
293+
}
294+
295+
const req = HttpMocks.createRequest({
296+
method: 'GET',
297+
url: '/'+op.input.endpoint,
298+
body: (op.input.data) ? JSON.parse(op.msg.toString()) : undefined
299+
})
300+
301+
const res = HttpMocks.createResponse()
302+
303+
debug('\tthe request', req)
304+
305+
debug('req ip type', typeof req.ip)
306+
307+
const route = this.party.hostRunner.router.get(op.input.endpoint)
308+
309+
debug('route',route)
310+
311+
debug('call route', await route._events.route({
312+
method: req.method,
313+
pathname: req.url,
314+
request: req,
315+
response: res
316+
}))
317+
318+
//debug('route via runner router')
319+
320+
//await this.party.hostRunner.onRequest(req, res)
321+
322+
op.result = {result: res._getData() }
323+
324+
debug('got result', op.result)
325+
326+
op.setState(HostOp.STATES.Finished_Success)
327+
return
328+
329+
} else if(op.input.endpoint == 'api-v2-peer-bouncer'){
280330

281331
debug('ask->',op.input.data)
282-
op.result = await this.party.handleCall(op.input.data)
332+
op.result = {result: await this.party.handleCall(op.input.data) }
283333

284334
op.setState(HostOp.STATES.Finished_Success)
285335

src/party/peer/peer-party.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const IParty = require('../iparty')
1212
*/
1313
class PeerParty extends IParty {
1414

15-
constructor ({comms, hostParty, qbOptions={debounce: 10, find_dedup:true, timeout: 10000}, ...options}) {
15+
constructor ({comms, hostParty, hostRunner, qbOptions={debounce: 10, find_dedup:true, timeout: 10000}, ...options}) {
1616
super(options)
1717

1818
this.comms = comms
@@ -26,9 +26,11 @@ class PeerParty extends IParty {
2626
})
2727

2828
this.hostParty = null
29+
this.hostRunner = null
2930

3031
if(this.comms.host){
3132
this.hostParty = hostParty
33+
this.hostRunner = hostRunner
3234
}
3335
}
3436

src/service/service-runner-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class ServiceRunnerNode {
144144

145145
this.endpoint[name] = endpoint
146146

147-
this.router.add(name, this.endpointHandler(endpoint))
147+
this.router.add(name, name, this.endpointHandler(endpoint))
148148
dt.end()
149149
debug('loaded endpoint',name,'in',dt.deltaMs,'ms')
150150
}
@@ -233,7 +233,7 @@ class ServiceRunnerNode {
233233
async onRequest(req, res){
234234
debug('onRequest')
235235

236-
debug('req', req.method, req.hostname, req.url, req.ips, req.body)
236+
debug('req', req.method, req.hostname,'-', req.url, req.ips, req.body)
237237

238238

239239

0 commit comments

Comments
 (0)