Skip to content

Commit 1c93064

Browse files
committed
service host
1 parent 39a9ef7 commit 1c93064

8 files changed

Lines changed: 155 additions & 27 deletions

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@
2121
"@dataparty/crypto": "1.0.1",
2222
"@zeit/ncc": "^0.22.3",
2323
"ajv": "6.9.1",
24+
"body-parser": "^1.19.0",
2425
"bson-objectid": "^1.3.0",
2526
"colors": "1.3.1",
27+
"cors": "^2.8.5",
2628
"debug": "^3.1.0",
2729
"deep-set": "^1.0.1",
2830
"dom-storage": "^2.1.0",
31+
"express": "^4.17.1",
32+
"express-list-routes": "^0.1.4",
2933
"git-repo-info": "^2.1.1",
3034
"jsonpath-plus": "^0.20.1",
3135
"last-eventemitter": "^1.1.1",
3236
"lodash": "^4.17.4",
3337
"lokijs": "1.5.6",
3438
"mkdirp": "^0.5.1",
39+
"morgan": "^1.10.0",
3540
"nconf": "^0.10.0",
3641
"node-persist": "^3.0.1",
3742
"nodemon-webpack-plugin": "^3.0.1",
@@ -44,6 +49,7 @@
4449
"touch": "^3.1.0",
4550
"url-parse": "^1.4.7",
4651
"uuid": "^3.2.1",
52+
"vm2": "^3.9.2",
4753
"websocket": "^1.0.26",
4854
"wreck": "14.0.2",
4955
"wrtc": "^0.4.4"

src/service/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
IService: require('./iservice'),
77
IEndpoint: require('./iendpoint'),
88
IMiddleware: require('./imiddleware'),
9+
ServiceHost: require('./service-host'),
910
middleware: {
1011
pre: {
1112
decrypt: require('./middleware/pre/decrypt')

src/service/iservice.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module.exports = class IService {
106106
}
107107

108108

109-
async compile(outputPath){
109+
async compile(outputPath, writeFile=true){
110110

111111
if(!outputPath){
112112
throw new Error('no output path')
@@ -127,14 +127,16 @@ module.exports = class IService {
127127

128128
this.compiled.middleware_order = this.middleware_order
129129

130-
const buildOutput = outputPath+'/'+ this.compiled.package.name.replace('/', '-') +'.dataparty-service.json'
131-
fs.writeFileSync(buildOutput, JSON.stringify(this.compiled))
130+
if(writeFile){
131+
const buildOutput = outputPath+'/'+ this.compiled.package.name.replace('/', '-') +'.dataparty-service.json'
132+
fs.writeFileSync(buildOutput, JSON.stringify(this.compiled))
132133

133-
const schemaOutput = outputPath+'/'+ this.compiled.package.name.replace('/', '-') +'.dataparty-schema.json'
134-
fs.writeFileSync(schemaOutput, JSON.stringify({
135-
package: this.compiled.package,
136-
...this.compiled.schemas
137-
}))
134+
const schemaOutput = outputPath+'/'+ this.compiled.package.name.replace('/', '-') +'.dataparty-schema.json'
135+
fs.writeFileSync(schemaOutput, JSON.stringify({
136+
package: this.compiled.package,
137+
...this.compiled.schemas
138+
}))
139+
}
138140

139141
return this.compiled
140142

@@ -173,7 +175,7 @@ module.exports = class IService {
173175
// provide a custom cache path or disable caching
174176
cache: false,
175177
// externals to leave as requires of the build
176-
//externals: ["externalpackage"],
178+
externals: ['debug', '@dataparty/crypto', '@hapi/joi', '@hapi/hoek'],
177179
// directory outside of which never to emit assets
178180
//filterAssetBase: process.cwd(), // default
179181
minify: false, // default

src/service/middleware/pre/decrypt.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const debug = require('debug')('roshub.middleware.pre.decrypt')
66
const IMiddleware = require('../../imiddleware')
77

88
module.exports = class Decrypt extends IMiddleware {
9-
constructor(opts){ super(opts) }
109

1110
static get Name(){
1211
return 'decrypt'

src/service/service-host.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1+
const CORS = require('cors')
2+
const Pify = require('pify')
3+
const {URL} = require('url')
14
const net = require('net')
25
const http = require('http')
36
const https = require('https')
4-
5-
const Pify = require('pify')
6-
const debug = require('debug')('roshub.server')
7-
const Hoek = require('hoek')
87
const morgan = require('morgan')
98
const express = require('express')
10-
const CORS = require('cors')
11-
const expressListRoutes = require('express-list-routes')
129
const bodyParser = require('body-parser')
13-
const {URL} = require('url');
10+
const expressListRoutes = require('express-list-routes')
11+
const debug = require('debug')('dataparty.service-host')
12+
1413

1514

1615
class ServiceHost {
1716
constructor({
18-
listenUri = 'http://0.0.0.0:4001',
19-
cors = null,
20-
trust_proxy = false
21-
}){
17+
cors = {},
18+
trust_proxy = false,
19+
listenUri = 'http://0.0.0.0:4001'
20+
}={}){
2221
this.apiApp = express()
2322
this.router = express.Router()
2423

25-
2624
if(cors){
2725
this.apiApp.use(CORS())
2826
this.apiApp.options('*', CORS(cors))
@@ -42,6 +40,10 @@ class ServiceHost {
4240
this.apiServerUri = new URL(listenUri)
4341
}
4442

43+
async onRequest(req, res){
44+
debug('request')
45+
res.end('nope')
46+
}
4547

4648
async start(){
4749

@@ -50,13 +52,13 @@ class ServiceHost {
5052
if(this.apiServer==null){
5153
debug('adding default endpoints')
5254
//Setup router
53-
//this.apiApp.use(this.router);
55+
this.apiApp.use(this.onRequest.bind(this))
5456

5557
if(debug.enabled){ expressListRoutes('API:', this.router ) }
5658
}
5759

5860
let listenPort = this.apiServerUri.port
59-
let listenHost = this.apiServerUri.host
61+
let listenHost = this.apiServerUri.hostname
6062

6163
if(this.apiServerUri.protocol == 'http:'){
6264

@@ -77,7 +79,11 @@ class ServiceHost {
7779

7880
}
7981

80-
await (Pify(this.apiServer.listen)(listenPort, listenHost))
82+
debug('server listening', this.apiServerUri.toString())
83+
84+
await new Promise((resolve,reject)=>{
85+
this.apiServer.listen(listenPort, listenHost, resolve)
86+
})
8187

8288
clearTimeout(this.errorHandlerTimer)
8389
this.errorHandlerTimer = null

src/service/service-runner.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const {VM, VMScript} = require('vm2')
2+
const debug = require('debug')('dataparty.server-runner')
3+
4+
5+
class CodeAccessor {
6+
constructor(code){
7+
this.script = new VMScript(code)
8+
}
9+
}
10+
11+
class ServiceRunner {
12+
constructor({service}){
13+
//
14+
}
15+
16+
async buildEndpointVM(name){ /**/ }
17+
18+
async buildMiddlewareVM(name){ /**/ }
19+
20+
async onRequest(req, res){
21+
debug('onRequest')
22+
}
23+
}
24+

test/test-service-compile.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const Path = require('path')
2-
const debug = require('debug')('test.server-db')
2+
const debug = require('debug')('test.service-compile')
33
const Dataparty = require('../src')
44

5+
const {VM, NodeVM, VMScript} = require('vm2')
6+
57
class ExampleService extends Dataparty.IService {
68
constructor(opts){
79
super(opts)
@@ -11,13 +13,70 @@ class ExampleService extends Dataparty.IService {
1113

1214
}
1315

16+
class CodeAccessor {
17+
constructor(code){
18+
debug('code-accessor', code)
19+
this.script = new VMScript(code)
20+
debug('compiled')
21+
}
22+
23+
run(context, sandbox){
24+
debug('run')
25+
26+
27+
let vm = new NodeVM({
28+
sandbox,
29+
require: {
30+
external: {
31+
modules: ['debug', '@dataparty/crypto', '@hapi/joi', '@hapi/hoek']
32+
},
33+
//builtin: ['*']
34+
}
35+
})
36+
37+
debug('has run')
38+
let fn = vm.run(this.script)
39+
const retVal = fn(context)
40+
debug('retVal', retVal)
41+
debug('context', context)
42+
return retVal
43+
}
44+
45+
}
46+
47+
class MiddlewareInfoAccessor extends CodeAccessor {
48+
constructor(code){
49+
super(`
50+
51+
let lib = ${code}
52+
53+
module.exports = ()=>{
54+
55+
return {
56+
Name: lib.Name,
57+
Type: lib.Type,
58+
Description: lib.Description,
59+
ConfigSchema: lib.ConfigSchema
60+
}
61+
}
62+
`)
63+
}
64+
65+
}
66+
1467
async function main(){
1568

1669
console.log(Object.keys(Dataparty))
1770

1871
const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })
1972

20-
const build = await service.compile(Path.join(__dirname,'../dataparty'))
73+
const build = await service.compile(Path.join(__dirname,'../dataparty'), false)
74+
75+
//debug(build.middleware.pre.decrypt)
76+
77+
let accessor = new MiddlewareInfoAccessor(build.middleware.pre.decrypt.code)
78+
79+
console.log(accessor.run('derp'))
2180

2281
console.log(build)
2382

test/test-service-host.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const Path = require('path')
2+
const debug = require('debug')('test.server-db')
3+
const Dataparty = require('../src')
4+
5+
/*class ExampleService extends Dataparty.IService {
6+
constructor(opts){
7+
super(opts)
8+
9+
this.addMiddleware(Dataparty.middleware_paths.pre.decrypt)
10+
}
11+
12+
}*/
13+
14+
async function main(){
15+
16+
console.log(Object.keys(Dataparty))
17+
18+
const host = new Dataparty.ServiceHost()
19+
20+
await host.start()
21+
22+
console.log('started')
23+
24+
//process.exit()
25+
}
26+
27+
28+
29+
main().catch(err=>{
30+
console.error(err)
31+
})

0 commit comments

Comments
 (0)