forked from ibm-functions/runtime-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.js
More file actions
168 lines (154 loc) · 6.54 KB
/
runner.js
File metadata and controls
168 lines (154 loc) · 6.54 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Object which encapsulates a first-class function, the user code for
* an action.
*
* This file (runner.js) must currently live in root directory for nodeJsAction.
*/
var util = require('util');
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
function NodeActionRunner() {
// Use this ref inside closures etc.
var thisRunner = this;
this.userScriptMain = undefined;
// This structure is reset for every action invocation. It contains two fields:
// - completed; indicating whether the action has already signaled completion
// - next; a callback to be invoked with the result of the action.
// Note that { error: ... } results are still results.
var callback = {
completed : undefined,
next : function (result) { return; }
};
this.init = function(message) {
function assertMainIsFunction() {
if (typeof thisRunner.userScriptMain !== 'function') {
throw "Action entrypoint '" + message.main + "' is not a function.";
}
}
// Loading the user code.
if (message.binary) {
// The code is a base64-encoded zip file.
return unzipInTmpDir(message.code).then(function (moduleDir) {
if(!fs.existsSync(path.join(moduleDir, 'package.json')) &&
!fs.existsSync(path.join(moduleDir, 'index.js'))) {
return Promise.reject('Zipped actions must contain either package.json or index.js at the root.')
}
try {
thisRunner.userScriptMain = eval('require("' + moduleDir + '").' + message.main);
assertMainIsFunction();
// The value 'true' has no special meaning here;
// the successful state is fully reflected in the
// successful resolution of the promise.
return true;
} catch (e) {
return Promise.reject(e);
}
}).catch(function (error) {
return Promise.reject(error);
});
} else {
// The code is a plain old JS file.
try {
thisRunner.userScriptMain = eval('(function(){' + message.code + '\nreturn ' + message.main + '})()');
assertMainIsFunction();
// See comment above about 'true'; it has no specific meaning.
return Promise.resolve(true);
} catch (e) {
return Promise.reject(e);
}
}
};
// Returns a Promise with the result of the user code invocation.
// The Promise is rejected iff the user code throws.
this.run = function(args) {
return new Promise(
function (resolve, reject) {
callback.completed = undefined;
callback.next = resolve;
try {
var result = thisRunner.userScriptMain(args);
} catch (e) {
reject(e);
}
// Non-promises/undefined instantly resolve.
Promise.resolve(result).then(function (resolvedResult) {
// This happens, e.g. if you just have "return;"
if (typeof resolvedResult === "undefined") {
resolvedResult = {};
}
resolve(resolvedResult);
}).catch(function (error) {
// A rejected Promise from the user code maps into a
// successful promise wrapping a whisk-encoded error.
// Special case if the user just called `reject()`.
if (!error) {
resolve({ error: {}});
} else {
resolve({ error: error });
}
});
}
);
};
// Helper function to copy a base64-encoded zip file to a temporary location,
// decompress it into temporary directory, and return the name of that directory.
// Note that this makes heavy use of shell commands because:
// 1) Node 0.12 doesn't have many of the useful fs functions.
// 2) We know in which environment we're running.
function unzipInTmpDir(base64) {
var mkTempCmd = "mktemp -d XXXXXXXX";
return exec(mkTempCmd).then(function (tmpDir1) {
return new Promise(
function (resolve, reject) {
var zipFile = path.join(tmpDir1, "action.zip");
fs.writeFile(zipFile, base64, "base64", function (err) {
if (err) {
reject("There was an error reading the action archive.");
}
resolve(zipFile);
});
}
);
}).then(function (zipFile) {
return exec(mkTempCmd).then(function (tmpDir2) {
return exec("unzip -qq " + zipFile + " -d " + tmpDir2).then(function (res) {
return path.resolve(tmpDir2);
}).catch(function (error) {
return Promise.reject("There was an error uncompressing the action archive.");
});
});
});
}
// Helper function to run shell commands.
function exec(cmd) {
return new Promise(
function (resolve, reject) {
child_process.exec(cmd, function (error, stdout, stderr) {
if (error) {
reject(stderr.trim());
} else {
resolve(stdout.trim());
}
});
}
);
}
}
module.exports = NodeActionRunner;