UNPKG

2.59 kBtext/coffeescriptView Raw
1Base = require('../base')
2
3debug = Base.logger('ix-caller')
4
5exportedProps = null
6
7
8exports.ixCall = (callData, responder, invoker) ->
9 timeoutSecs = Number(callData.timeout || Base.env.IX_PROXY_TIMEOUT)
10 if !(timeoutSecs > 0) # careful about NaN / Number(undefined)
11 timeoutSecs = 30 # default 30 seconds
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
36exports.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 # // 2015-01-23:
46 # // Support coffeescript more easily, don't support return values.
47 # ////
48 # // if (returnVal !== void(0)) {
49 # // // JSON doesn't easily differentiate between (undefined) and (null),
50 # // // so we can't transmit (undefined) across the wire.
51 # // // Therefore, use (null) as the "don't wait for the callback" value
52 # // // (instead of using a special Base.IX_RETURN_UNDEFINED value, that
53 # // // we try to convert to (undefined) when transmitting it back).
54 # // //clientCallback(null, returnVal === Base.IX_RETURN_UNDEFINED ? void(0) : returnVal)
55 # // // Always be async. http://nodejs.org/api/process.html#process_process_nexttick_callback
56 # // process.nextTick(function(){ clientCallback(null, returnVal) })
57 # // }
58 else
59 debug('ixCallNodejs', "Property not exported: '"+callData.api+"'.")
60 throw 'Invalid function: '+callData.api
61
62exports.setNodejsExports = (exportedPropsInput) ->
63 exportedProps = exportedPropsInput
64
65findProp = (name) ->
66 names = name.split('.')
67 findPropHelper(names, exportedProps)
68
69findPropHelper = (names, props) ->
70 name = names.shift()
71 prop = props[name]
72 if prop && names.length > 0
73 prop = findPropHelper(names, prop)
74 prop