Skip to content

Commit eea9194

Browse files
authored
Merge pull request #18 from datapartyjs/service-runner
initial service runner
2 parents 0a538a9 + 4d3447c commit eea9194

20 files changed

Lines changed: 486 additions & 45 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.0.7",
4+
"version": "1.1.0",
55
"main": "src/index.js",
66
"browser": "src/index-browser.js",
77
"files": [
@@ -40,6 +40,7 @@
4040
"nconf": "^0.10.0",
4141
"node-persist": "^3.0.1",
4242
"nodemon-webpack-plugin": "^3.0.1",
43+
"origin-router": "^1.6.4",
4344
"parse-url": "^5.0.1",
4445
"prompt": "^1.0.0",
4546
"roslib": "^0.20.0",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const debug = require('debug')('dataparty.EndpointInfoSandbox')
2+
const Sandbox = require('./sandbox')
3+
4+
class EndpointInfoSandbox extends Sandbox {
5+
constructor(code){
6+
super(`
7+
8+
module.exports = async ()=>{
9+
10+
class ErrorError extends Error {
11+
constructor(msg){
12+
super()
13+
this.code = 'ErrorError'
14+
this.message = 'You did not throw an error object, always throw an Error object! - [' + msg + ']'
15+
}
16+
}
17+
18+
try{
19+
let Lib = ${code}
20+
21+
return {
22+
Name: Lib.Name,
23+
Description: Lib.Description,
24+
MiddlewareConfig: Lib.MiddlewareConfig
25+
}
26+
}
27+
catch(err){
28+
29+
if(!err || !err.stack){
30+
err = new ErrorError(err)
31+
}
32+
33+
throw err
34+
}
35+
36+
}
37+
`)
38+
39+
this.info = null
40+
}
41+
42+
async run(){
43+
44+
debug('running')
45+
46+
this.info = await super.run()
47+
48+
return this.info
49+
}
50+
51+
}
52+
53+
module.exports = EndpointInfoSandbox

src/sandbox/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = {
22
Sandbox: require('./sandbox'),
33
SandboxError: require('./sandbox-error'),
4-
MiddlewareInfoSandbox: require('./middleware-info-sandbox')
4+
EndpointInfoSandbox: require('./endpoint-info-sandbox'),
5+
MiddlewareInfoSandbox: require('./middleware-info-sandbox'),
6+
MiddlewareExecSandbox: require('./middleware-exec-sandbox')
57
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const debug = require('debug')('dataparty.MiddlewareExecSandbox')
2+
const Sandbox = require('./sandbox')
3+
4+
class MiddlewareExecSandbox extends Sandbox {
5+
constructor(code, func='run'){
6+
super(`
7+
8+
module.exports = async (ctx, static_ctx)=>{
9+
10+
class ErrorError extends Error {
11+
constructor(msg){
12+
super()
13+
this.code = 'ErrorError'
14+
this.message = 'You did not throw an error object, always throw an Error object! - [' + msg + ']'
15+
}
16+
}
17+
18+
try{
19+
let Lib = ${code}
20+
21+
return await Lib.${func}(ctx, static_ctx)
22+
}
23+
catch(err){
24+
25+
if(!err || !err.stack){
26+
err = new ErrorError(err)
27+
}
28+
29+
throw err
30+
}
31+
32+
}
33+
`)
34+
35+
this.result = null
36+
}
37+
38+
async run(ctx, static_ctx){
39+
40+
debug('running')
41+
42+
this.result = await super.run(ctx, static_ctx)
43+
44+
return this.result
45+
}
46+
47+
}
48+
49+
module.exports = MiddlewareExecSandbox

src/sandbox/middleware-info-sandbox.js

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

44
class MiddlewareInfoSandbox extends Sandbox {
@@ -42,7 +42,7 @@ module.exports = async ()=>{
4242

4343
async run(){
4444

45-
45+
debug('running', this.code)
4646
this.info = await super.run()
4747

4848
return this.info

src/sandbox/sandbox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const SandboxError = require('./sandbox-error')
55

66
class Sandbox {
77
constructor(code){
8-
debug('compiling code')
8+
debug('compiling code', typeof code)
99
this.code = code
1010
this.script = new VMScript(code)
1111
debug('compiled')

src/service/endpoint-context.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const Debug = require('debug')
2+
3+
class EndpointContext {
4+
constructor({party, endpoint, req, res}){
5+
this.party = party
6+
this.endpoint = endpoint
7+
this.MiddlewareConfig = endpoint.info.MiddlewareConfig
8+
9+
this.req = req
10+
this.res = res
11+
this.actor = null
12+
this.stats = {
13+
start: Date.now(),
14+
bytes_in: !req ? null : JSON.stringify(req.body).length
15+
}
16+
this.oauth_cloud = null
17+
this.session = null
18+
this.identity = null
19+
this.input = null
20+
this.input_session_id = null
21+
this._debug = Debug('dataparty.context.undefined')
22+
this._debugContent = []
23+
}
24+
25+
setReq(req){ this.req = req }
26+
setRes(res){ this.res = res }
27+
setCloud(cloud){ this.cloud = cloud }
28+
29+
setSession(session){
30+
this.session = session
31+
this._debug = Debug('dataparty.context.' + session.id)
32+
}
33+
34+
setOauthCloud(oauth_cloud){
35+
this.oauth_cloud = oauth_cloud
36+
this._debug = Debug('oauth cloud', oauth_cloud.id)
37+
}
38+
39+
setIdentity(identity){ this.identity = identity }
40+
setActor(actor){ this.actor = actor }
41+
setInputSession(input_session_id){ this.input_session_id = input_session_id }
42+
43+
/*async applyMiddleware(){
44+
45+
}*/
46+
47+
debug(msg, ...args){
48+
let line = ((new Error().stack).split('at ')[2]).trim()
49+
50+
const openParen = line.indexOf('(') + 1
51+
const closeParen = line.indexOf(')')
52+
53+
const filePath = line.substring(openParen, closeParen).replace(__dirname, '')
54+
line = filePath
55+
56+
const newMsg = line + ' ' + msg
57+
58+
this._debugContent.push({
59+
file: filePath,
60+
time: Date.now(),
61+
msg: msg + ' ' + args.map(v=>{return JSON.stringify(v)}).join(' ')
62+
})
63+
64+
this._debug(newMsg, ...args)
65+
}
66+
}
67+
68+
module.exports = EndpointContext

src/service/endpoint-runner.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const IRunner = require('./irunner')
2+
3+
const EndpointInfoSandbox = require('../sandbox/endpoint-info-sandbox')
4+
const MiddlewareExecSandbox = require('../sandbox/middleware-exec-sandbox')
5+
6+
class EndpointRunner extends IRunner {
7+
constructor(code){
8+
super({
9+
info: new EndpointInfoSandbox(code),
10+
exec: new MiddlewareExecSandbox(code),
11+
start: new MiddlewareExecSandbox(code,'start')
12+
})
13+
}
14+
}
15+
16+
module.exports = EndpointRunner
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const Joi = require('@hapi/joi')
2+
const Hoek = require('@hapi/hoek')
3+
const {Message, Routines} = require('@dataparty/crypto')
4+
const debug = require('debug')('roshub.endpoint.identity')
5+
6+
const IEndpoint = require('../iendpoint')
7+
8+
module.exports = class ServiceIdentity extends IEndpoint {
9+
10+
static get Name(){
11+
return 'identity'
12+
}
13+
14+
15+
static get Description(){
16+
return 'Get host identity'
17+
}
18+
19+
static get MiddlewareConfig(){
20+
return {
21+
pre: {
22+
decrpyt: false
23+
}
24+
}
25+
}
26+
27+
static async run(ctx, static_ctx){
28+
29+
30+
const identity = ctx.party.identity
31+
32+
return {
33+
...identity
34+
}
35+
}
36+
}

src/service/iendpoint.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module.exports = class IEndpoint {
1515
throw new Error('not implemented')
1616
}
1717

18+
static async start(party){
19+
20+
}
21+
1822
static async run(context){
1923
throw new Error('not implemented')
2024
}

0 commit comments

Comments
 (0)