Skip to content

Commit 2330a5b

Browse files
authored
Merge pull request #19 from datapartyjs/sandbox-source-map
Sandbox source + Middleware running
2 parents eea9194 + ee89096 commit 2330a5b

32 files changed

Lines changed: 730 additions & 113 deletions

package.json

Lines changed: 4 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": [
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"@dataparty/bouncer-db": "1.0.1",
2121
"@dataparty/crypto": "1.0.1",
22+
"@hapi/joi": "^17.1.1",
2223
"@zeit/ncc": "^0.22.3",
2324
"ajv": "6.9.1",
2425
"body-parser": "^1.19.0",
@@ -46,6 +47,7 @@
4647
"roslib": "^0.20.0",
4748
"sanitize-filename": "^1.6.3",
4849
"simple-peer": "^9.7.2",
50+
"source-map": "^0.7.3",
4951
"store-js": "^2.0.4",
5052
"touch": "^3.1.0",
5153
"url-parse": "^1.4.7",
@@ -70,7 +72,7 @@
7072
},
7173
"repository": {
7274
"type": "git",
73-
"url": "https://github.com/dataparty/dataparty-api.git"
75+
"url": "https://github.com/datapartyjs/dataparty-api.git"
7476
},
7577
"author": "RosHub Inc. <code@roshub.io>",
7678
"license": "Apache-2.0"

public/app.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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('secure-echo', {t:(new Date()).getTime()}, {
69+
expectClearTextReply: false,
70+
sendClearTextRequest: false,
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("secure-echo-speed").innerHTML = text
79+
80+
}, 250)
81+
82+
83+
setInterval(async ()=>{
84+
//console.log('send')
85+
86+
87+
let callTime2 = new DeltaTime()
88+
89+
callTime2.start()
90+
91+
const echoReply2 = await party.comms.call('echo', {t:(new Date()).getTime()}, {
92+
expectClearTextReply: true,
93+
sendClearTextRequest: true,
94+
useSessions: false
95+
})
96+
97+
callTime2.end()
98+
complete++
99+
100+
const text = `call deltaMs=<i>${callTime2.deltaMs}</i>ms complete ${complete}`
101+
document.getElementById("echo-speed").innerHTML = text
102+
103+
}, 100)
104+
105+
}
106+
107+
108+
main().then(console.log).catch(console.log)

public/crypto-test.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
<div>
10+
<i>/echo</i> <p id="echo-speed"></p>
11+
</div>
12+
<br/>
13+
<div>
14+
<i>/secure-echo</i> <p id="secure-echo-speed"></p>
15+
</div>
16+
</body>
17+
</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()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = class MiddlewareValidationError extends Error{
2+
constructor(err, parsed){
3+
super(err.toString())
4+
5+
this.name = 'MiddlewareValidationError'
6+
this.parsed = parsed
7+
}
8+
}

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/party/iparty.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ class IParty {
200200

201201
if (!cfgIdenStr){
202202
debug('generated new identity')
203-
this._identity = new dataparty_crypto.Identity({id: 'primary'})
204-
await this.config.write(path, this._identity.toJSON(true))
203+
204+
await this.resetIdentity()
205205
} else {
206206
debug('loaded identity')
207207
this._identity = dataparty_crypto.Identity.fromString(JSON.stringify(cfgIdenStr))
@@ -211,6 +211,10 @@ class IParty {
211211
async resetIdentity(){
212212
const path = 'identity'
213213
await this.config.write(path, null)
214+
215+
this._identity = new dataparty_crypto.Identity({id: 'primary'})
216+
await this.config.write(path, this._identity.toJSON(true))
217+
214218
await this.loadIdentity()
215219
}
216220

src/sandbox/endpoint-info-sandbox.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const debug = require('debug')('dataparty.EndpointInfoSandbox')
22
const Sandbox = require('./sandbox')
33

44
class EndpointInfoSandbox extends Sandbox {
5-
constructor(code){
5+
constructor(code, map){
66
super(`
77
88
module.exports = async ()=>{
@@ -34,9 +34,10 @@ module.exports = async ()=>{
3434
}
3535
3636
}
37-
`)
37+
`, map)
3838

3939
this.info = null
40+
this.payloadLines = code.split('\n').length-1
4041
}
4142

4243
async run(){

src/sandbox/middleware-exec-sandbox.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const debug = require('debug')('dataparty.MiddlewareExecSandbox')
22
const Sandbox = require('./sandbox')
33

44
class MiddlewareExecSandbox extends Sandbox {
5-
constructor(code, func='run'){
6-
super(`
5+
constructor(code, map, func='run'){
6+
super(`
77
88
module.exports = async (ctx, static_ctx)=>{
99
@@ -30,16 +30,17 @@ module.exports = async (ctx, static_ctx)=>{
3030
}
3131
3232
}
33-
`)
33+
`, map)
3434

3535
this.result = null
36+
this.payloadLines = code.split('\n').length-1
3637
}
3738

38-
async run(ctx, static_ctx){
39+
async run(ctx, config){
3940

4041
debug('running')
4142

42-
this.result = await super.run(ctx, static_ctx)
43+
this.result = await super.run(ctx, {Config: config})
4344

4445
return this.result
4546
}

0 commit comments

Comments
 (0)