Skip to content

Commit

Permalink
Merge pull request #21 from datapartyjs/snapshot
Browse files Browse the repository at this point in the history
Service Host
  • Loading branch information
sevenbitbyte authored Nov 6, 2020
2 parents 2330a5b + 58ff6be commit 9bb337f
Show file tree
Hide file tree
Showing 17 changed files with 493 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dataparty/api",
"private": false,
"version": "1.1.1",
"version": "1.2.0",
"main": "src/index.js",
"browser": "src/index-browser.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/errors/middleware-validation-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = class MiddlewareValidationError extends Error{
constructor(err, parsed){
super(err.toString())

this.name = 'MiddlewareValidationError'
this.name = 'ValidationError'
this.parsed = parsed
}
}
4 changes: 2 additions & 2 deletions src/sandbox/middleware-exec-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ module.exports = async (ctx, static_ctx)=>{
this.payloadLines = code.split('\n').length-1
}

async run(ctx, config){
async run(ctx, static_ctx){

debug('running')

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

return this.result
}
Expand Down
7 changes: 3 additions & 4 deletions src/sandbox/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ const SandboxError = require('./sandbox-error')

class Sandbox {
constructor(code, map, offsetToken=' let Lib = '){
debug('compiling code', typeof code)
debug('compiling code', code.length, 'Bytes')
this.code = code
this.map = map

this.mappings = null
this.payloadLines = 0
this.offsetToken = offsetToken
this.offset = code.split(this.offsetToken)[0].split('\n').length-1
this.script = new VMScript(code)
debug('compiled')
this.script = new VMScript(code).compile()
}

async loadSourceMap(){
Expand All @@ -27,7 +26,7 @@ class Sandbox {
}

async run(context, sandbox){
debug('running')
//debug('running')
try{

let vm = new NodeVM({
Expand Down
3 changes: 3 additions & 0 deletions src/service/endpoint-context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const Debug = require('debug')

const DeltaTime = require('../utils/delta-time')

class EndpointContext {
constructor({party, endpoint, req, res, input, debug=Debug, sendFullErrors=false}){
this.party = party
Expand Down Expand Up @@ -29,6 +31,7 @@ class EndpointContext {

this._debug = debug('dataparty.context.undefined')
this._debugContent = []
this.dt = new DeltaTime().start()
}

setReq(req){ this.req = req }
Expand Down
42 changes: 42 additions & 0 deletions src/service/endpoints/service-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const Joi = require('@hapi/joi')
const Hoek = require('@hapi/hoek')
const {Message, Routines} = require('@dataparty/crypto')
const debug = require('debug')('dataparty.endpoint.version')

const IEndpoint = require('../iendpoint')

module.exports = class ServiceVersion extends IEndpoint {

static get Name(){
return 'version'
}


static get Description(){
return 'Get service version'
}

static get MiddlewareConfig(){
return {
pre: {
decrypt: false,
validate: Joi.object().keys({}).description('no input allowed'),
},
post:{
validate: Joi.object().keys({
name: Joi.string(),
branch: Joi.string(),
version: Joi.string(),
githash: Joi.string()
})
}
}
}

static async run(ctx, static_ctx){

return {
...Package
}
}
}
6 changes: 4 additions & 2 deletions src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ module.exports = {
endpoint: {
echo: require('./endpoints/echo'),
secureecho: require('./endpoints/secure-echo'),
identity: require('./endpoints/service-identity')
identity: require('./endpoints/service-identity'),
version: require('./endpoints/service-version')
},
endpoint_paths: {
echo: Path.join(__dirname, './endpoints/echo.js'),
secureecho: Path.join(__dirname, './endpoints/secure-echo.js'),
identity: Path.join(__dirname, './endpoints/service-identity.js')
identity: Path.join(__dirname, './endpoints/service-identity.js'),
version: Path.join(__dirname, './endpoints/service-version.js')
}
}
6 changes: 5 additions & 1 deletion src/service/ischema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const debug = require('debug')('dataparty.service.ISchema')

const MgoUtils = require('../utils/mongoose-scheme-utils')

module.exports = class ISchema {
constructor(){ }
Expand Down Expand Up @@ -33,6 +33,10 @@ module.exports = class ISchema {
throw new Error('not implemented')
}

static get Utils(){
return MgoUtils
}

/*static get Class(){
throw new Error('not implemented')
}*/
Expand Down
8 changes: 7 additions & 1 deletion src/service/iservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Hoek = require('@hapi/hoek')
const {JSONPath} = require('jsonpath-plus')
const gitRepoInfo = require('git-repo-info')
const BouncerDb = require('@dataparty/bouncer-db')
const mongoose = BouncerDb.mongoose()
const json2ts = require('json-schema-to-typescript')
const debug = require('debug')('dataparty.service.IService')

Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = class IService {
* @param {dataparty.service.ISchema} schema_path
*/
addSchema(schema_path){
debug('addSchema', schema_path)
const schema = require(schema_path)
const name = schema.Type

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

addDocument(document_path){
debug('addDocument', document_path)
const document = require(document_path)
const name = document.DocumentSchema

Expand Down Expand Up @@ -118,6 +121,8 @@ module.exports = class IService {
this.compiled.package.githash = info.sha
this.compiled.package.branch = info.branch

debug('compiling sources',this.sources)

await Promise.all([
this.compileMiddleware('pre'),
this.compileMiddleware('post'),
Expand Down Expand Up @@ -146,7 +151,6 @@ module.exports = class IService {
async compileList(field, outputPath){
// Build file list
debug('compileList',field)
debug(this.sources)
for(const name in this.sources[field]){
debug('\r', field, name)

Expand Down Expand Up @@ -203,7 +207,9 @@ module.exports = class IService {


async compileSchemas(buildTypeScript=false){
debug('compileSchema')
for(let key in this.constructors.schemas){
debug('\tcompiling', key)
const model = this.constructors.schemas[key]
let schema = mongoose.Schema(model.Schema)
schema = model.setupSchema(schema)
Expand Down
27 changes: 25 additions & 2 deletions src/service/service-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const MiddlewareRunner = require('./middleware-runner')
const EndpointContext = require('./endpoint-context')
const EndpointRunner = require('./endpoint-runner')

const DeltaTime = require('../utils/delta-time')

const Router = require('origin-router').Router

class ServiceRunner {
Expand Down Expand Up @@ -45,6 +47,8 @@ class ServiceRunner {
}

debug('loadEndpoint', name)

let dt = new DeltaTime().start()
const build = Hoek.reach(this.service, `compiled.endpoints.${name}`)
let endpoint = new EndpointRunner(build.code, build.map)

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

this.router.add(name, this.endpointHandler(endpoint))
dt.end()
debug('loaded endpoint',name,'in',dt.deltaMs,'ms')
}


Expand All @@ -85,6 +91,7 @@ class ServiceRunner {

debug('loadMiddleware', type, name)

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

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

this.middleware[type][name] = runner

dt.end()
debug('loaded middleware',name,'in',dt.deltaMs,'ms')

return runner
}

Expand Down Expand Up @@ -127,6 +137,8 @@ class ServiceRunner {

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

debug('req done')


if(!route){
res.status(404).end()
Expand Down Expand Up @@ -158,13 +170,16 @@ class ServiceRunner {

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

const result = await endpoint.run(context)
const result = await endpoint.run(context, {Package: this.service.compiled.package})

context.setOutput(result)

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

context.dt.end()

/*debug('ctx.log', context._debugContent)*/
debug('ran endpoint', endpoint.info.Name, 'in', context.dt.deltaMs, 'ms')
debug('result', context.output)

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

context.dt.end()

debug('crashed (',endpoint.info.Name,') in', context.dt.deltaMs, 'ms')

context.res.status(500).send({
error: {
code: err.code,
Expand Down Expand Up @@ -202,7 +221,11 @@ class ServiceRunner {
debug('\t\trunning', name)
const middleware = Hoek.reach(this.middleware, `${type}.${name}`)

await middleware.run(ctx, info)
const dt = new DeltaTime().start()
await middleware.run(ctx, {Config: info})
dt.end()

debug('runMiddleware(',type,name,') in', dt.deltaMs, 'ms')
}
}
}
Expand Down
Loading

0 comments on commit 9bb337f

Please sign in to comment.