-
Notifications
You must be signed in to change notification settings - Fork 43
/
FunctionLoader.ts
89 lines (79 loc) · 3.27 KB
/
FunctionLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { isObject, isFunction } from 'util';
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
import { FunctionInfo } from './FunctionInfo'
import { InternalException } from "./utils/InternalException";
export interface IFunctionLoader {
load(functionId: string, metadata: rpc.IRpcFunctionMetadata): void;
getInfo(functionId: string): FunctionInfo;
getFunc(functionId: string): Function;
}
export class FunctionLoader implements IFunctionLoader {
private _loadedFunctions: { [k: string]: {
info: FunctionInfo,
func: Function
}} = {};
load(functionId: string, metadata: rpc.IRpcFunctionMetadata): void {
if (metadata.isProxy === true) {
return;
}
let scriptFilePath = <string>(metadata && metadata.scriptFile);
let script = require(scriptFilePath);
let entryPoint = <string>(metadata && metadata.entryPoint);
let userFunction = getEntryPoint(script, entryPoint);
if(!isFunction(userFunction)) {
throw new InternalException("The resolved entry point is not a function and cannot be invoked by the functions runtime. Make sure the function has been correctly exported.");
}
this._loadedFunctions[functionId] = {
info: new FunctionInfo(metadata),
func: userFunction
};
}
getInfo(functionId: string): FunctionInfo {
let loadedFunction = this._loadedFunctions[functionId];
if (loadedFunction && loadedFunction.info) {
return loadedFunction.info;
} else {
throw new InternalException(`Function info for '${functionId}' is not loaded and cannot be invoked.`);
}
}
getFunc(functionId: string): Function {
let loadedFunction = this._loadedFunctions[functionId];
if (loadedFunction && loadedFunction.func) {
return loadedFunction.func;
} else {
throw new InternalException(`Function code for '${functionId}' is not loaded and cannot be invoked.`);
}
}
}
function getEntryPoint(f: any, entryPoint?: string): Function {
if (isObject(f)) {
var obj = f;
if (entryPoint) {
// the module exports multiple functions
// and an explicit entry point was named
f = f[entryPoint];
}
else if (Object.keys(f).length === 1) {
// a single named function was exported
var name = Object.keys(f)[0];
f = f[name];
}
else {
// finally, see if there is an exported function named
// 'run' or 'index' by convention
f = f.run || f.index;
}
if (isFunction(f)){
return function() {
return f.apply(obj, arguments);
}
}
}
if (!f) {
let msg = (entryPoint ? `Unable to determine function entry point: ${entryPoint}. `: "Unable to determine function entry point. ") + "If multiple functions are exported, " +
"you must indicate the entry point, either by naming it 'run' or 'index', or by naming it " +
"explicitly via the 'entryPoint' metadata property.";
throw new InternalException(msg);
}
return f;
}