@@ -2,7 +2,11 @@ const Path = require('path')
22const debug = require ( 'debug' ) ( 'test.service-compile' )
33const Dataparty = require ( '../src' )
44
5- const { VM , NodeVM, VMScript} = require ( 'vm2' )
5+ //const {VM, NodeVM, VMScript} = require('vm2')
6+
7+ process . on ( 'uncaughtException' , ( err ) => {
8+ console . error ( 'Asynchronous error caught.' , err ) ;
9+ } )
610
711class ExampleService extends Dataparty . IService {
812 constructor ( opts ) {
@@ -13,72 +17,38 @@ class ExampleService extends Dataparty.IService {
1317
1418}
1519
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- }
6620
6721async function main ( ) {
6822
69- console . log ( Object . keys ( Dataparty ) )
23+ debug ( Object . keys ( Dataparty ) )
7024
7125 const service = new ExampleService ( { name : '@dataparty/example' , version : '0.0.1' } )
7226
7327 const build = await service . compile ( Path . join ( __dirname , '../dataparty' ) , false )
7428
7529 //debug(build.middleware.pre.decrypt)
7630
77- let accessor = new MiddlewareInfoAccessor ( build . middleware . pre . decrypt . code )
31+ let decryptInfo = new Dataparty . MiddlewareInfoSandbox ( build . middleware . pre . decrypt . code )
32+
33+ try {
34+ await decryptInfo . run ( )
35+ }
36+ catch ( err ) {
37+ debug ( 'supressing error' )
38+ debug ( '\tname\t' , err . name )
39+ debug ( '\tcode\t' , err . code )
40+
41+ debug ( '\tmsg\t' , err . message )
7842
79- console . log ( accessor . run ( 'derp' ) )
43+ debug ( '\tstack\t' , err . stack )
44+
45+ debug ( '\tlocations\t' , err . locations )
46+ }
8047
81- console . log ( build )
48+ debug ( '# Middleware #' )
49+ debug ( '\t Name=' , decryptInfo . info . Name )
50+ debug ( '\t Type="' , decryptInfo . info . Type , '"' )
51+ debug ( '\t Desc="' , decryptInfo . info . Description , '"' )
8252
8353 process . exit ( )
8454}
0 commit comments