Skip to content

Commit 9bb337f

Browse files
authored
Merge pull request #21 from datapartyjs/snapshot
Service Host
2 parents 2330a5b + 58ff6be commit 9bb337f

17 files changed

Lines changed: 493 additions & 14 deletions

package.json

Lines changed: 1 addition & 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.1.1",
4+
"version": "1.2.0",
55
"main": "src/index.js",
66
"browser": "src/index-browser.js",
77
"files": [

src/errors/middleware-validation-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = class MiddlewareValidationError extends Error{
22
constructor(err, parsed){
33
super(err.toString())
44

5-
this.name = 'MiddlewareValidationError'
5+
this.name = 'ValidationError'
66
this.parsed = parsed
77
}
88
}

src/sandbox/middleware-exec-sandbox.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ module.exports = async (ctx, static_ctx)=>{
3636
this.payloadLines = code.split('\n').length-1
3737
}
3838

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

4141
debug('running')
4242

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

4545
return this.result
4646
}

src/sandbox/sandbox.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ const SandboxError = require('./sandbox-error')
66

77
class Sandbox {
88
constructor(code, map, offsetToken=' let Lib = '){
9-
debug('compiling code', typeof code)
9+
debug('compiling code', code.length, 'Bytes')
1010
this.code = code
1111
this.map = map
1212

1313
this.mappings = null
1414
this.payloadLines = 0
1515
this.offsetToken = offsetToken
1616
this.offset = code.split(this.offsetToken)[0].split('\n').length-1
17-
this.script = new VMScript(code)
18-
debug('compiled')
17+
this.script = new VMScript(code).compile()
1918
}
2019

2120
async loadSourceMap(){
@@ -27,7 +26,7 @@ class Sandbox {
2726
}
2827

2928
async run(context, sandbox){
30-
debug('running')
29+
//debug('running')
3130
try{
3231

3332
let vm = new NodeVM({

src/service/endpoint-context.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const Debug = require('debug')
22

3+
const DeltaTime = require('../utils/delta-time')
4+
35
class EndpointContext {
46
constructor({party, endpoint, req, res, input, debug=Debug, sendFullErrors=false}){
57
this.party = party
@@ -29,6 +31,7 @@ class EndpointContext {
2931

3032
this._debug = debug('dataparty.context.undefined')
3133
this._debugContent = []
34+
this.dt = new DeltaTime().start()
3235
}
3336

3437
setReq(req){ this.req = req }
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.endpoint.version')
5+
6+
const IEndpoint = require('../iendpoint')
7+
8+
module.exports = class ServiceVersion extends IEndpoint {
9+
10+
static get Name(){
11+
return 'version'
12+
}
13+
14+
15+
static get Description(){
16+
return 'Get service version'
17+
}
18+
19+
static get MiddlewareConfig(){
20+
return {
21+
pre: {
22+
decrypt: false,
23+
validate: Joi.object().keys({}).description('no input allowed'),
24+
},
25+
post:{
26+
validate: Joi.object().keys({
27+
name: Joi.string(),
28+
branch: Joi.string(),
29+
version: Joi.string(),
30+
githash: Joi.string()
31+
})
32+
}
33+
}
34+
}
35+
36+
static async run(ctx, static_ctx){
37+
38+
return {
39+
...Package
40+
}
41+
}
42+
}

src/service/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ module.exports = {
3434
endpoint: {
3535
echo: require('./endpoints/echo'),
3636
secureecho: require('./endpoints/secure-echo'),
37-
identity: require('./endpoints/service-identity')
37+
identity: require('./endpoints/service-identity'),
38+
version: require('./endpoints/service-version')
3839
},
3940
endpoint_paths: {
4041
echo: Path.join(__dirname, './endpoints/echo.js'),
4142
secureecho: Path.join(__dirname, './endpoints/secure-echo.js'),
42-
identity: Path.join(__dirname, './endpoints/service-identity.js')
43+
identity: Path.join(__dirname, './endpoints/service-identity.js'),
44+
version: Path.join(__dirname, './endpoints/service-version.js')
4345
}
4446
}

src/service/ischema.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const debug = require('debug')('dataparty.service.ISchema')
2-
2+
const MgoUtils = require('../utils/mongoose-scheme-utils')
33

44
module.exports = class ISchema {
55
constructor(){ }
@@ -33,6 +33,10 @@ module.exports = class ISchema {
3333
throw new Error('not implemented')
3434
}
3535

36+
static get Utils(){
37+
return MgoUtils
38+
}
39+
3640
/*static get Class(){
3741
throw new Error('not implemented')
3842
}*/

src/service/iservice.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Hoek = require('@hapi/hoek')
55
const {JSONPath} = require('jsonpath-plus')
66
const gitRepoInfo = require('git-repo-info')
77
const BouncerDb = require('@dataparty/bouncer-db')
8+
const mongoose = BouncerDb.mongoose()
89
const json2ts = require('json-schema-to-typescript')
910
const debug = require('debug')('dataparty.service.IService')
1011

@@ -64,6 +65,7 @@ module.exports = class IService {
6465
* @param {dataparty.service.ISchema} schema_path
6566
*/
6667
addSchema(schema_path){
68+
debug('addSchema', schema_path)
6769
const schema = require(schema_path)
6870
const name = schema.Type
6971

@@ -72,6 +74,7 @@ module.exports = class IService {
7274
}
7375

7476
addDocument(document_path){
77+
debug('addDocument', document_path)
7578
const document = require(document_path)
7679
const name = document.DocumentSchema
7780

@@ -118,6 +121,8 @@ module.exports = class IService {
118121
this.compiled.package.githash = info.sha
119122
this.compiled.package.branch = info.branch
120123

124+
debug('compiling sources',this.sources)
125+
121126
await Promise.all([
122127
this.compileMiddleware('pre'),
123128
this.compileMiddleware('post'),
@@ -146,7 +151,6 @@ module.exports = class IService {
146151
async compileList(field, outputPath){
147152
// Build file list
148153
debug('compileList',field)
149-
debug(this.sources)
150154
for(const name in this.sources[field]){
151155
debug('\r', field, name)
152156

@@ -203,7 +207,9 @@ module.exports = class IService {
203207

204208

205209
async compileSchemas(buildTypeScript=false){
210+
debug('compileSchema')
206211
for(let key in this.constructors.schemas){
212+
debug('\tcompiling', key)
207213
const model = this.constructors.schemas[key]
208214
let schema = mongoose.Schema(model.Schema)
209215
schema = model.setupSchema(schema)

src/service/service-runner.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const MiddlewareRunner = require('./middleware-runner')
88
const EndpointContext = require('./endpoint-context')
99
const EndpointRunner = require('./endpoint-runner')
1010

11+
const DeltaTime = require('../utils/delta-time')
12+
1113
const Router = require('origin-router').Router
1214

1315
class ServiceRunner {
@@ -45,6 +47,8 @@ class ServiceRunner {
4547
}
4648

4749
debug('loadEndpoint', name)
50+
51+
let dt = new DeltaTime().start()
4852
const build = Hoek.reach(this.service, `compiled.endpoints.${name}`)
4953
let endpoint = new EndpointRunner(build.code, build.map)
5054

@@ -63,6 +67,8 @@ class ServiceRunner {
6367
this.endpoint[name] = endpoint
6468

6569
this.router.add(name, this.endpointHandler(endpoint))
70+
dt.end()
71+
debug('loaded endpoint',name,'in',dt.deltaMs,'ms')
6672
}
6773

6874

@@ -85,6 +91,7 @@ class ServiceRunner {
8591

8692
debug('loadMiddleware', type, name)
8793

94+
let dt = new DeltaTime().start()
8895
const build = Hoek.reach(this.service, `compiled.middleware.${type}.${name}`)
8996

9097
if(!build || !build.code){
@@ -99,6 +106,9 @@ class ServiceRunner {
99106

100107
this.middleware[type][name] = runner
101108

109+
dt.end()
110+
debug('loaded middleware',name,'in',dt.deltaMs,'ms')
111+
102112
return runner
103113
}
104114

@@ -127,6 +137,8 @@ class ServiceRunner {
127137

128138
let route = await this.router.route(req, res)
129139

140+
debug('req done')
141+
130142

131143
if(!route){
132144
res.status(404).end()
@@ -158,13 +170,16 @@ class ServiceRunner {
158170

159171
await this.runMiddleware(middlewareCfg, context, 'pre')
160172

161-
const result = await endpoint.run(context)
173+
const result = await endpoint.run(context, {Package: this.service.compiled.package})
162174

163175
context.setOutput(result)
164176

165177
await this.runMiddleware(middlewareCfg, context, 'post')
166178

179+
context.dt.end()
180+
167181
/*debug('ctx.log', context._debugContent)*/
182+
debug('ran endpoint', endpoint.info.Name, 'in', context.dt.deltaMs, 'ms')
168183
debug('result', context.output)
169184

170185
context.res.send(context.output)
@@ -173,6 +188,10 @@ class ServiceRunner {
173188
catch(err){
174189
debug('caught error', err)
175190

191+
context.dt.end()
192+
193+
debug('crashed (',endpoint.info.Name,') in', context.dt.deltaMs, 'ms')
194+
176195
context.res.status(500).send({
177196
error: {
178197
code: err.code,
@@ -202,7 +221,11 @@ class ServiceRunner {
202221
debug('\t\trunning', name)
203222
const middleware = Hoek.reach(this.middleware, `${type}.${name}`)
204223

205-
await middleware.run(ctx, info)
224+
const dt = new DeltaTime().start()
225+
await middleware.run(ctx, {Config: info})
226+
dt.end()
227+
228+
debug('runMiddleware(',type,name,') in', dt.deltaMs, 'ms')
206229
}
207230
}
208231
}

0 commit comments

Comments
 (0)