@@ -19,14 +19,46 @@ function helix_wrap_action(main) {
1919 const { pipe } = require ( 'MOD_PIPE' ) ;
2020 const { pre, before, after } = require ( 'MOD_PRE' ) ;
2121
22+ // todo: mode to helix-pipeline
23+ const CONTEXT_PROPS = [ 'error' , 'request' , 'content' , 'response' ] ;
24+ const CONTENT_PROPS = [ 'sources' , 'body' , 'mdast' , 'sections' , 'document' , 'htast' , 'json' , 'xml' , 'meta' , 'title' , 'intro' , 'image' ] ;
25+ const REQUEST_PROPS = [ 'url' , 'path' , 'pathInfo' , 'rootPath' , 'selector' , 'extension' , 'method' , 'headers' , 'params' ] ;
26+ const RESPONSE_PROPS = [ 'status' , 'body' , 'hast' , 'headers' ] ;
27+
28+ const filterObject = ( obj , allowedProperties ) => {
29+ if ( ! obj ) {
30+ return ;
31+ }
32+ Object . keys ( obj ) . forEach ( ( key ) => {
33+ if ( allowedProperties . indexOf ( key ) < 0 ) {
34+ delete obj [ key ] ;
35+ }
36+ } )
37+ } ;
38+
39+ const sanitizeContext = ( context ) => {
40+ filterObject ( context , CONTEXT_PROPS ) ;
41+ filterObject ( context . content , CONTENT_PROPS ) ;
42+ filterObject ( context . request , REQUEST_PROPS ) ;
43+ filterObject ( context . response , RESPONSE_PROPS ) ;
44+ } ;
45+
2246 // this gets called by openwhisk
2347 return async function wrapped ( params ) {
2448 // this is the once function that will be installed in the pipeline
25- async function once ( payload , action ) {
49+ async function once ( context , action ) {
2650 // calls the pre function and then the script's main.
2751 async function invoker ( next ) {
28- const ret = await Promise . resolve ( pre ( payload , action ) ) ;
29- return Promise . resolve ( next ( ret || payload , action ) ) ;
52+ const ret = await Promise . resolve ( pre ( context , action ) ) ;
53+ const res = await Promise . resolve ( next ( ret || context , action ) ) ;
54+ if ( res && res . response && res . response . body ) {
55+ if ( ! context . response ) {
56+ context . response = { } ;
57+ }
58+ context . response . body = res . response . body ;
59+ }
60+ sanitizeContext ( context ) ;
61+ return context ;
3062 }
3163 return invoker ( main ) ;
3264 }
0 commit comments