1 | Base = require('../base')
|
2 |
|
3 | debug = Base.logger('ix-caller')
|
4 |
|
5 | exportedProps = null
|
6 |
|
7 |
|
8 | exports.ixCall = (callData, responder, invoker) ->
|
9 | timeoutSecs = Number(callData.timeout || Base.env.IX_PROXY_TIMEOUT)
|
10 | if !(timeoutSecs > 0)
|
11 | timeoutSecs = 30
|
12 | wait = timeoutSecs * 1000
|
13 | timerOn = true
|
14 | timer = setTimeout ->
|
15 | if timerOn
|
16 | timerOn = false
|
17 | err = 'TIMEOUT: Callback not invoked after '+(wait/1000)+' seconds, aborting. (Start server with the environment variable FAAS_IX_PROXY_TIMEOUT set to customize the timeout.)'
|
18 | debug('ixCall', err)
|
19 | responder({ reason: err })
|
20 | , wait
|
21 | clientCallback = (err, data) ->
|
22 | debug('clientCallback', { err: err, data: data })
|
23 | if err instanceof Error
|
24 | err =
|
25 | name: err.name
|
26 | message: err.message
|
27 | if timerOn
|
28 | timerOn = false
|
29 | clearTimeout(timer)
|
30 | responder(err, data)
|
31 | try
|
32 | invoker(callData, clientCallback)
|
33 | catch e
|
34 | clientCallback(e)
|
35 |
|
36 | exports.ixCallNodejs = (callData, responder) ->
|
37 | debug('ixCallNodejs', callData)
|
38 | func = findProp(callData.api)
|
39 | if func
|
40 | exports.ixCall callData, responder, (cd, clientCallback) ->
|
41 | args = cd.args.slice(0)
|
42 | args.push(clientCallback)
|
43 | func.apply({}, args)
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | else
|
59 | debug('ixCallNodejs', "Property not exported: '"+callData.api+"'.")
|
60 | throw 'Invalid function: '+callData.api
|
61 |
|
62 | exports.setNodejsExports = (exportedPropsInput) ->
|
63 | exportedProps = exportedPropsInput
|
64 |
|
65 | findProp = (name) ->
|
66 | names = name.split('.')
|
67 | findPropHelper(names, exportedProps)
|
68 |
|
69 | findPropHelper = (names, props) ->
|
70 | name = names.shift()
|
71 | prop = props[name]
|
72 | if prop && names.length > 0
|
73 | prop = findPropHelper(names, prop)
|
74 | prop
|