Skip to content

Commit d19d0cc

Browse files
committed
echo example
1 parent bf52e3b commit d19d0cc

14 files changed

Lines changed: 225 additions & 39 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"main": "src/index.js",
66
"browser": "src/index-browser.js",
77
"files": [
@@ -72,7 +72,7 @@
7272
},
7373
"repository": {
7474
"type": "git",
75-
"url": "https://github.com/dataparty/dataparty-api.git"
75+
"url": "https://github.com/datapartyjs/dataparty-api.git"
7676
},
7777
"author": "RosHub Inc. <code@roshub.io>",
7878
"license": "Apache-2.0"

public/app.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const config = new DataParty.Config.LocalStorageConfig({
2+
cloud: {
3+
uri: 'http://localhost:4001'
4+
}
5+
})
6+
7+
const party = new DataParty.CloudParty({
8+
config,
9+
})
10+
11+
class DeltaTime {
12+
constructor(){
13+
this.startMs = null
14+
this.endMs = null
15+
this.deltaMs = null
16+
}
17+
18+
start(){
19+
this.startMs = (new Date).getTime()
20+
}
21+
22+
end(){
23+
this.endMs = (new Date).getTime()
24+
25+
this.deltaMs = this.endMs - this.startMs
26+
}
27+
28+
}
29+
30+
async function main(){
31+
32+
try{
33+
await party.start()
34+
console.log('party started')
35+
}
36+
catch(err){
37+
console.log('crash', err)
38+
}
39+
40+
//const identityReply = await party.comms.call('identity', undefined, true)
41+
42+
let firstCallTime = new DeltaTime()
43+
44+
firstCallTime.start()
45+
46+
const echoReply = await party.comms.call('echo', {t:(new Date()).getTime()}, {
47+
expectClearTextReply: true,
48+
sendClearTextRequest: true,
49+
useSessions: false
50+
})
51+
52+
firstCallTime.end()
53+
54+
console.log('server key', party.comms.remoteIdentity)
55+
console.log('echo response', echoReply)
56+
console.log('first call time', firstCallTime)
57+
58+
let complete = 0
59+
60+
setInterval(async ()=>{
61+
//console.log('send')
62+
63+
64+
let callTime2 = new DeltaTime()
65+
66+
callTime2.start()
67+
68+
const echoReply2 = await party.comms.call('echo', {t:(new Date()).getTime()}, {
69+
expectClearTextReply: true,
70+
sendClearTextRequest: true,
71+
useSessions: false
72+
})
73+
74+
callTime2.end()
75+
complete++
76+
77+
const text = `call deltaMs=<i>${callTime2.deltaMs}</i>ms complete ${complete}`
78+
document.getElementById("console").innerHTML = text
79+
80+
}, 75)
81+
82+
83+
}
84+
85+
86+
main().then(console.log).catch(console.log)

public/crypto-test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<script src='../dist/dataparty-browser.js'></script>
4+
<script src='./app.js'></script>
5+
</head>
6+
7+
8+
<body>
9+
<p id="console"></p>
10+
</body>
11+
</html>

public/dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../dist/

src/comms/rest-comms.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class RestComms extends EventEmitter {
1414
this.uri = undefined
1515
this.wsUri = undefined
1616
this.cfgPrefix = 'rest'
17+
this.uriPrefix = ''
1718
this.config = config
1819
this.sessionId = undefined
1920
this.remoteIdentity = remoteIdentity
@@ -92,7 +93,13 @@ class RestComms extends EventEmitter {
9293
this.config.write(path, { id: this.sessionId })
9394
}
9495

95-
async call(path, data, expectClearTextReply = false) {
96+
async call(path, data,
97+
{
98+
expectClearTextReply = false,
99+
sendClearTextRequest = false,
100+
useSessions = true
101+
} = {}
102+
) {
96103
if (!this.uri) {
97104
await this.loadCloud()
98105
}
@@ -101,20 +108,33 @@ class RestComms extends EventEmitter {
101108
}
102109
await this.getServiceIdentity()
103110

104-
const obj = { session: this.sessionId, data: data }
111+
//const obj = { session: this.sessionId, data: data }
105112

106-
debug('call', path, ' req - ', JSON.stringify(obj))
113+
const fullPath = this.uri + this.uriPrefix + path
114+
107115

108-
const content = await this.party.encrypt(obj, this.remoteIdentity)
116+
let content = null
117+
118+
if(data || this.useSessions){
119+
content = {data}
120+
121+
if(useSessions){ content.session = this.sessionId }
122+
123+
if(!sendClearTextRequest){
124+
content = await this.party.encrypt(content, this.remoteIdentity)
125+
}
126+
}
127+
128+
debug('call', fullPath, ' req - ', JSON.stringify(content))
109129

110130
let reply
111131
try {
112-
const str = await RestComms.HttpPost(this.uri + path, content)
132+
const str = await RestComms.HttpPost(fullPath, content)
113133
reply = JSON.parse(str)
114134

115135
// debug('raw reply ->', reply)
116136
} catch (error) {
117-
debug('rest', path, ' call fail ->', error.message)
137+
debug('rest', fullPath, ' call fail ->', error.message)
118138
throw new Error('RestCommsError')
119139
}
120140

@@ -124,13 +144,13 @@ class RestComms extends EventEmitter {
124144
expectClearTextReply
125145
)
126146

127-
debug('call', path, ' res - ', JSON.stringify(msg, null, 2))
147+
debug('call', fullPath, ' res - ', JSON.stringify(msg, null, 2))
128148

129149
return msg
130150
}
131151

132152
async syncActors() {
133-
const info = await this.call('api-v2-actor-info')
153+
const info = await this.call('actor-info')
134154

135155
debug('syncActors - got info', JSON.stringify(info, null, 2))
136156

@@ -177,7 +197,7 @@ class RestComms extends EventEmitter {
177197
if (!this.uri) {
178198
await this.loadCloud()
179199
}
180-
const serverIdentity = await RestComms.HttpGet(this.uri + 'api-v2-identity')
200+
const serverIdentity = await RestComms.HttpGet(this.uri + `${this.uriPrefix}identity`)
181201
debug('server identity - ', serverIdentity)
182202

183203
this.remoteIdentity = dataparty_crypto.Identity.fromString(serverIdentity)
@@ -199,7 +219,7 @@ class RestComms extends EventEmitter {
199219

200220
async redeemInvite(code) {
201221
// await this.party.loadIdentity()
202-
return this.call('api-v2-claim-user-invite', {
222+
return this.call('claim-user-invite', {
203223
short_code: code
204224
})
205225
}
@@ -214,7 +234,7 @@ class RestComms extends EventEmitter {
214234
return this.party
215235
.loadIdentity()
216236
.then(() => {
217-
return this.call('api-v2-oauth-github', { code: code })
237+
return this.call('oauth-github', { code: code })
218238
})
219239
.then(sessionInfo => {
220240
debug(sessionInfo)
@@ -280,7 +300,7 @@ class RestComms extends EventEmitter {
280300
debug('actor', this.party.actor)
281301

282302
try {
283-
const reply = await this.call('api-v2-rest-session', {
303+
const reply = await this.call('rest-session', {
284304
actor: {
285305
id: this.party.actor.id,
286306
type: this.party.actor.type
@@ -307,7 +327,7 @@ class RestComms extends EventEmitter {
307327
return this.websocketComm
308328
}
309329

310-
return this.call('api-v2-websocket-session').then(reply => {
330+
return this.call('websocket-session').then(reply => {
311331
debug(reply)
312332
if (!this.wsUri) {
313333
this.loadCloud()

src/party/document-factory.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class DocumentFactory {
1414
this.documentClass = documentClass || IDocument
1515
this.validators = {}
1616

17-
for(let schema of this.model.JSONSchema){
18-
const v = this.ajv.compile(schema)
19-
this.validators[schema.title] = v
20-
debug(schema.title)
17+
if(this.model){
18+
for(let schema of this.model.JSONSchema){
19+
const v = this.ajv.compile(schema)
20+
this.validators[schema.title] = v
21+
debug(schema.title)
22+
}
2123
}
2224
}
2325

src/service/endpoint-context.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class EndpointContext {
2323
this.output = null
2424
this.outputError = null
2525

26+
this.senderKey = null
27+
2628
this.sendFullErrors = sendFullErrors
2729

2830
this._debug = debug('dataparty.context.undefined')
@@ -33,34 +35,39 @@ class EndpointContext {
3335
setRes(res){ this.res = res }
3436
setCloud(cloud){ this.cloud = cloud }
3537

38+
setSenderKey(key){
39+
this.
40+
this.senderKey = key
41+
}
42+
3643
setSession(session){
3744
this.session = session
38-
this._debug = Debug('dataparty.context.' + session.id)
45+
this.debug('session.id' + session.id)
3946
}
4047

4148
setOauthCloud(oauth_cloud){
4249
this.oauth_cloud = oauth_cloud
43-
this._debug = Debug('oauth cloud', oauth_cloud.id)
50+
this.debug('oauth cloud', oauth_cloud.id)
4451
}
4552

4653
setInput(input){
4754
this.input = input
48-
this._debug = Debug('input set')
55+
this.debug('input set')
4956
}
5057

5158
setOutput(output){
5259
this.output = output
53-
this._debug = Debug('output set')
60+
this.debug('output set')
5461
}
5562

5663
setInputError(error){
5764
this.inputError = error
58-
this._debug = Debug('input error', error)
65+
this.debug('input error', error)
5966
}
6067

6168
setOutputError(error){
6269
this.outputError = error
63-
this._debug = Debug('output error', error)
70+
this.debug('output error', error)
6471
}
6572

6673
setIdentity(identity){ this.identity = identity }

src/service/endpoints/echo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = class EchoEndpoint extends IEndpoint {
2323
validate: Joi.object().keys(null).description('any input allowed'),
2424
},
2525
post: {
26-
/*encrypt: false,*/
26+
encrypt: false,
2727
validate: Joi.object().keys(null).description('any output allowed')
2828
}
2929
}

src/service/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module.exports = {
1717
validate: require('./middleware/pre/validate')
1818
},
1919
post: {
20-
validate: require('./middleware/post/validate.js')
20+
validate: require('./middleware/post/validate.js'),
21+
encrypt: require('./middleware/post/encrypt')
2122
}
2223
},
2324
middleware_paths: {
@@ -26,7 +27,8 @@ module.exports = {
2627
validate: Path.join(__dirname, './middleware/pre/validate.js')
2728
},
2829
post: {
29-
validate: Path.join(__dirname, './middleware/post/validate.js')
30+
validate: Path.join(__dirname, './middleware/post/validate.js'),
31+
encrypt: Path.join(__dirname, './middleware/post/encrypt.js')
3032
}
3133
},
3234
endpoint: {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const Joi = require('@hapi/joi')
2+
const Hoek = require('@hapi/hoek')
3+
const {Message, Routines} = require('@dataparty/crypto')
4+
const debug = require('debug')('dataparty.middleware.post.encrypt')
5+
6+
const IMiddleware = require('../../imiddleware')
7+
8+
module.exports = class Encrypt extends IMiddleware {
9+
10+
static get Name(){
11+
return 'encrypt'
12+
}
13+
14+
static get Type(){
15+
return 'post'
16+
}
17+
18+
static get Description(){
19+
return 'Decrypt inbound data'
20+
}
21+
22+
static get ConfigSchema(){
23+
return Joi.boolean()
24+
}
25+
26+
static async start(party){
27+
28+
}
29+
30+
static async run(context){
31+
32+
if (!Hoek.reach(context, 'endpoint.MiddlewareConfig.post.encrypt', false)){
33+
return
34+
}
35+
36+
37+
const msg = new Message(context.input)
38+
const jsonContent = await msg.decrpyt(this.serviceParty.privateIdentity)
39+
const publicKeys = Routines.extractPublicKeys(msg.enc)
40+
41+
context.setInputSession(jsonContent.session)
42+
43+
return {
44+
input: Hoek.reach(content, 'data'),
45+
sender: {
46+
type: 'ecdsa',
47+
public: publicKeys
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)