11'use strict' ;
22
3- const wrapper = require ( './util/result-wrapper' ) ,
4- Backendless = require ( 'backendless' ) ;
3+ const wrapper = require ( './util/result-wrapper' ) ;
4+ const Backendless = require ( 'backendless' ) ;
55
6- const executors = {
7- [ 'com.backendless.coderunner.commons.protocol.RequestMethodInvocation' ] : './invoke-method' ,
8- [ 'com.backendless.coderunner.commons.protocol.RequestActionInvocation' ] : './invoke-action' ,
9- [ 'com.backendless.coderunner.commons.protocol.RequestServiceInvocation' ] : './invoke-service'
10- } ;
6+ const SHUTDOWN_CODE = 32768 ;
7+ const SHUTDOWN_ACTION = 'SHUTDOWN' ;
118
129/**
1310 * @typedef {Object } InitAppData
@@ -28,49 +25,74 @@ const executors = {
2825
2926const executor = module . exports = { } ;
3027
28+ executor . RMI = 'com.backendless.coderunner.commons.protocol.RequestMethodInvocation' ;
29+ executor . RAI = 'com.backendless.coderunner.commons.protocol.RequestActionInvocation' ;
30+ executor . RSI = 'com.backendless.coderunner.commons.protocol.RequestServiceInvocation' ;
31+
32+ const executors = {
33+ [ executor . RMI ] : './invoke-handler' ,
34+ [ executor . RAI ] : './invoke-action' ,
35+ [ executor . RSI ] : './invoke-service'
36+ } ;
37+
3138/**
3239 * @param {?Error|ExceptionWrapper|String } err
3340 * @param {* } result
3441 * @returns {String }
3542 */
3643function invocationResult ( err , result ) {
37- return JSON . stringify ( wrapper . invocationResult ( err , result ) ) ;
44+ return JSON . stringify ( wrapper . invocationResult ( err , result ) ) ;
3845}
3946
4047/**
4148 * @param {CodeRunnerTask } task
4249 * @returns {Function } task executor
4350 */
4451function getTaskExecutor ( task ) {
45- const taskClass = task . ___jsonclass ;
52+ const taskClass = task . ___jsonclass ;
4653
47- if ( ! executors [ taskClass ] ) {
48- throw new Error ( `Unknown task [${ taskClass } ]` ) ;
49- }
54+ if ( ! executors [ taskClass ] ) {
55+ throw new Error ( `Unknown task [${ taskClass } ]` ) ;
56+ }
5057
51- return require ( executors [ taskClass ] ) ;
58+ return require ( executors [ taskClass ] ) ;
5259}
5360
5461/**
5562 * @param {CodeRunnerTask } task
5663 */
5764function initClientSdk ( task ) {
65+ if ( task . initAppData ) {
5866 Backendless . serverURL = task . initAppData . url ;
5967 Backendless . initApp ( task . applicationId , task . initAppData . secretKey , task . initAppData . appVersionName ) ;
68+ }
69+ }
70+
71+ /**
72+ * A temporal workaround for http://bugs.backendless.com/browse/BKNDLSS-12041
73+ *
74+ * @param {CodeRunnerTask } task
75+ */
76+ function fixTask ( task ) {
77+ if ( task . ___jsonclass === executor . RMI && task . eventId === SHUTDOWN_CODE ) {
78+ task . ___jsonclass = executor . RAI ;
79+ task . actionType = SHUTDOWN_ACTION ;
80+ }
6081}
6182
6283/**
6384 * @param {CodeRunnerTask } task
6485 * @param {Object } runnerOpts
65- * @param {? ServerCodeModel } model
86+ * @param {ServerCodeModel= } model
6687 * @returns {Promise.<string> } InvocationResult JSON string
6788 */
68- executor . execute = function ( task , runnerOpts , model ) {
89+ executor . execute = function ( task , runnerOpts , model ) {
90+ return Promise . resolve ( )
91+ . then ( ( ) => initClientSdk ( task ) )
92+ . then ( ( ) => fixTask ( task ) )
93+ . then ( ( ) => getTaskExecutor ( task ) )
94+ . then ( ( taskExecutor ) => taskExecutor ( task , runnerOpts , model ) )
95+ . then ( result => result && invocationResult ( null , result ) )
96+ . catch ( invocationResult ) ;
97+ } ;
6998
70- return Promise . resolve ( )
71- . then ( ( ) => initClientSdk ( task ) )
72- . then ( ( ) => getTaskExecutor ( task ) )
73- . then ( ( taskExecutor ) => taskExecutor ( task , runnerOpts , model ) )
74- . then ( result => result && invocationResult ( null , result ) )
75- . catch ( invocationResult ) ;
76- } ;
0 commit comments