Skip to content

Commit fc34ec7

Browse files
committed
initial service runner
1 parent 27d5c52 commit fc34ec7

9 files changed

Lines changed: 320 additions & 22 deletions

File tree

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+
Type: Lib.Type,
24+
Description: Lib.Description,
25+
MiddlewareConfig: Lib.MiddlewareConfig
26+
}
27+
}
28+
catch(err){
29+
30+
if(!err || !err.stack){
31+
err = new ErrorError(err)
32+
}
33+
34+
throw err
35+
}
36+
37+
}
38+
`)
39+
40+
this.info = null
41+
}
42+
43+
async run(){
44+
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
Sandbox: require('./sandbox'),
33
SandboxError: require('./sandbox-error'),
4-
MiddlewareInfoSandbox: require('./middleware-info-sandbox')
4+
MiddlewareInfoSandbox: require('./middleware-info-sandbox'),
5+
MiddlewareExecSandbox: require('./middleware-exec-sandbox')
56
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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, party}){
39+
40+
41+
this.result = await super.run(ctx, static_ctx)
42+
43+
return this.result
44+
}
45+
46+
}
47+
48+
module.exports = MiddlewareExecSandbox

src/service/endpoint-context.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 = actor
12+
this.stats = {
13+
start: Date.now(),
14+
bytes_in: !req ? null : JSON.stringify(req.body).length
15+
}
16+
this.oauth_cloud = oauth_cloud
17+
this.session = session
18+
this.identity = identity
19+
this.input = input
20+
this.input_session_id = input_session_id
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+
applyMiddleware
44+
45+
debug(msg, ...args){
46+
let line = ((new Error().stack).split('at ')[2]).trim()
47+
48+
const openParen = line.indexOf('(') + 1
49+
const closeParen = line.indexOf(')')
50+
51+
const filePath = line.substring(openParen, closeParen).replace(__dirname, '')
52+
line = filePath
53+
54+
const newMsg = line + ' ' + msg
55+
56+
this._debugContent.push({
57+
file: filePath,
58+
time: Date.now(),
59+
msg: msg + ' ' + args.map(v=>{return JSON.stringify(v)}).join(' ')
60+
})
61+
62+
this._debug(newMsg, ...args)
63+
}
64+
}
65+
66+
module.exports = EndpointContext

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
}

src/service/imiddleware.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module.exports = class IMiddleware {
1919
throw new Error('not implemented')
2020
}
2121

22+
static async start(party){
23+
24+
}
25+
2226
static async run(context){
2327
throw new Error('not implemented')
2428
}

src/service/middleware/pre/decrypt.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,18 @@ module.exports = class Decrypt extends IMiddleware {
2323
return Joi.boolean()
2424
}
2525

26-
async run(config, context){
27-
if(!config){
28-
return
29-
}
26+
static async run(ctx, static_ctx){
3027

31-
if (!Hoek.reach(context, 'endpoint.MiddlewareConfig.pre.decrypt', false)){
32-
return Promise.resolve(context)
28+
if (!Hoek.reach(ctx, 'endpoint.MiddlewareConfig.pre.decrypt', false)){
29+
return
3330
}
3431

3532

36-
const msg = new Message(context.input)
33+
const msg = new Message(ctx.input)
3734
const jsonContent = await msg.decrpyt(this.serviceParty.privateIdentity)
3835
const publicKeys = Routines.extractPublicKeys(msg.enc)
3936

40-
context.setInputSession(jsonContent.session)
37+
ctx.setInputSession(jsonContent.session)
4138

4239
return {
4340
input: Hoek.reach(content, 'data'),

src/service/service-host.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ class ServiceHost {
1616
constructor({
1717
cors = {},
1818
trust_proxy = false,
19-
listenUri = 'http://0.0.0.0:4001'
19+
listenUri = 'http://0.0.0.0:4001',
20+
runner
2021
}={}){
2122
this.apiApp = express()
23+
this.runner = runner
2224
this.router = express.Router()
2325

2426
if(cors){
@@ -40,19 +42,14 @@ class ServiceHost {
4042
this.apiServerUri = new URL(listenUri)
4143
}
4244

43-
async onRequest(req, res){
44-
debug('request')
45-
res.end('nope')
46-
}
47-
4845
async start(){
4946

5047
debug('starting server')
5148

5249
if(this.apiServer==null){
5350
debug('adding default endpoints')
5451
//Setup router
55-
this.apiApp.use(this.onRequest.bind(this))
52+
this.apiApp.use(this.runner.onRequest.bind(this.runner))
5653

5754
if(debug.enabled){ expressListRoutes('API:', this.router ) }
5855
}

0 commit comments

Comments
 (0)