This is an automated email from the ASF dual-hosted git repository.
lukeroy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-runtime-nodejs.git
The following commit(s) were added to refs/heads/master by this push:
new 7545841 Add tar.gz support to the nodejs runtime Proxy (#235)
7545841 is described below
commit 7545841b7f1f2a99faee85b2e9dbf0c9e4f56e82
Author: Luke-Roy-IBM <[email protected]>
AuthorDate: Tue Apr 11 08:09:56 2023 +0200
Add tar.gz support to the nodejs runtime Proxy (#235)
* Add tar.gz support to the nodejs runtime Proxy
This new feature will allow for more versatile deployment packages and
greater flexibility in handling compressed files. Giving an alternative to zip
files.
this feature does not impact the current functionality of the current proxy
* fix whitespace and eol
* whitespace + eol
* Change error message to handle test case There was an error uncompressing
the action archive.
---
core/nodejsActionBase/runner.js | 49 +++++++++++++++++++++++++++++++++++------
1 file changed, 42 insertions(+), 7 deletions(-)
diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js
index 6b5759c..5ed211c 100644
--- a/core/nodejsActionBase/runner.js
+++ b/core/nodejsActionBase/runner.js
@@ -27,7 +27,11 @@ const path = require('path');
function initializeActionHandler(message) {
if (message.binary) {
// The code is a base64-encoded zip file.
- return unzipInTmpDir(message.code)
+ ext = detectFileType(message.code)
+ if (ext == 'unsupported'){
+ return Promise.reject("There was an error uncompressing the action
archive.");
+ }
+ return extractInTmpDir(message.code)
.then(moduleDir => {
let parts = splitMainHandler(message.main);
if (parts === undefined) {
@@ -138,21 +142,33 @@ class NodeActionRunner {
* Note that this makes heavy use of shell commands because the environment is
expected
* to provide the required executables.
*/
-function unzipInTmpDir(zipFileContents) {
+function extractInTmpDir(archiveFileContents) {
const mkTempCmd = "mktemp -d XXXXXXXX";
return exec(mkTempCmd).then(tmpDir => {
return new Promise((resolve, reject) => {
- const zipFile = path.join(tmpDir, "action.zip");
- fs.writeFile(zipFile, zipFileContents, "base64", err => {
- if (!err) resolve(zipFile);
+ ext = detectFileType(archiveFileContents)
+ if (ext == 'unsupported'){
+ reject("There was an error Detecting the File type");
+ }
+ const archiveFile = path.join(tmpDir, "action."+ ext);
+ fs.writeFile(archiveFile, archiveFileContents, "base64", err => {
+ if (!err) resolve(archiveFile);
else reject("There was an error reading the action archive.");
});
});
- }).then(zipFile => {
+ }).then(archiveFile => {
return exec(mkTempCmd).then(tmpDir => {
- return exec("unzip -qq " + zipFile + " -d " + tmpDir)
+ if (ext === 'zip') {
+ return exec("unzip -qq " + archiveFile + " -d " + tmpDir)
+ .then(res => path.resolve(tmpDir))
+ .catch(error => Promise.reject("There was an error
uncompressing the action archive."));
+ } else if (ext === 'tar.gz') {
+ return exec("tar -xzf " + archiveFile + " -C " + tmpDir + " >
/dev/null")
.then(res => path.resolve(tmpDir))
.catch(error => Promise.reject("There was an error
uncompressing the action archive."));
+ } else {
+ return Promise.reject("There was an error uncompressing the
action archive.");
+ }
});
});
}
@@ -198,3 +214,22 @@ module.exports = {
NodeActionRunner,
initializeActionHandler
};
+
+// helper function to detect if base64string is zip or tar.gz
+// and returns the file ending
+function detectFileType(base64String) {
+ // Decode the base64 string into binary data
+ const binaryData = Buffer.from(base64String, 'base64');
+
+ // Examine the first few bytes of the binary data to determine the file
type
+ const magicNumber = binaryData.slice(0, 4).toString('hex');
+
+ if (magicNumber === '504b0304') {
+ return 'zip';
+ // GZIP: 1f8b0808 maximum compression level, 1f8b0800 default compression
+ } else if (magicNumber === '1f8b0808' || magicNumber === '1f8b0800') {
+ return 'tar.gz';
+ } else {
+ return 'unsupported';
+ }
+}