mimik Serverless APIs

mimik edge node light container manager provides a serverless programming environment and at the moment it only supports JavaScript programming language.


General Objects

Result

result.data

Type: <string>

Description: The result of the data. This is responding output of success function callback.


HTTP Serverless Objects

Context

This is the serverless context. The purpose of the context object is to provide other helper singleton to process a HTTP request. Below is an example of the list of singletons that can be accessed through the context.


                    var context = {
                      env, // the environment variable passed to the container 
                      storage, // please refer to the API document on storage
                      http, // please refer to the API document on http client
                      edge // please refer to the API document on edge API
                  };
                  

Env

This object holds the serverless environment variables. The env property contains the environment variable that passes into the serverless function. Below is an example showing the environment variables setup.


                      var env = {
                        clientId: "AbCdEfG1234",
                        clientSecret: "ThisisaSecret"
                    };
                  

Request

This object represents the client HTTP request made to the server. This request object follows very closely to the nodejs HTTP server request object.

request.url

Type: <string>

Description: The url of the request.

request.method

Type: <string>

Description: The HTTP method of the request, such as ‘GET’, ‘POST’, ‘DELETE’, ‘PUT’, and etc.

request.authorization

Type: <string>

Description: The HTTP header such as the 'Authorization Header'.

request.body

Type: <string>

Description: If the content-type is application/json of the client, this will be the body of the POST or PUT request.

request.handleFormRequest(formRequestOption)

Type: <function>

Argument:

Description: This functions is used to handle the HTTP POST with content type "multipart/form-data". The function takes an object formRequestOption from the developer to generate a list of required callbacks.

Example Code:


                        mimikModule.exports = function(context, req, res) {
                          var metadata = "";
                          var file = {};
                          req.handleFormRequest({
                              found: (key, filename) => {
                                  var todo = {
                                      action: 'skip'
                                  };
                                  if (key === 'metadata') {
                                      todo = {
                                          action: 'get'
                                      };
                                  } else if (key === 'file') {
                                      file.filename = filename;
                                      todo = {
                                          action: 'store',
                                          path: 'new-filename'
                                      };
                                  } else {
                                      todo = {
                                          action: 'abort'
                                      }
                                  }
                                  return todo;
                              },
                              get: (key, value) => {
                                  metadataBuf = metadata.concat(value);
                              },
                              store: (path, size) => {
                                  file.path = path;
                                  file.size = size;
                              },
                          });
                          res.end(value);
                      }
                    


FormRequestOption

This object passes a list that is a required callback function to the request.handleFormRequest() to handle the ‘HTTP Form POST’.

formRequestOption.found

Type: <function>

Argument:

  • key

    Type: <string>

  • filename

    Type: <string>

Description: This function callback is called whenever a new key is encountered. And you must return a ToDo to indicate what to do with the key. Please refer to formRequestToDo.action and formRequestToDo.path.

formRequestOption.get

Type: <function>

Argument:

  • key

    Type: <string>

  • value

    Type: <string>

Description: This callback is called when you indicate the method to deal with the key data is to get the data. The value of the key is returned as a string.

formRequestOption.store

Type: <function>

Argument:

  • path

    Type: <string>

  • size

    Type: <integer>

Description: This callback is called when you indicate the method to deal with the key data is to store the data.


FormRequestToDo

FormRequestToDo is an object that is used in request.handleFormRequest() to handle the ‘HTTP Form POST’.

formRequestToDo.action

Type: <string>

Description: The following actions are available:

  • abort: this will abort the entire form request
  • skip: this will skip the current key of the form request
  • store: this will handle the form request using the 'store' function provided in FormRequestOption
  • get: this will handle the form request using the 'get' function provided in FormRequestOption

formRequestToDo.path

Type: <string>

Description: If the formRequestToDo.action is store, one would need to know where to store the file. The formRequestToDo.path is the path to the file.


Response

This class represents the server HTTP response object. This is also similar to a NodeJs HTTP server response object.

response.statusCode

Type: <integer>

Description: HTTP response code. the default is 200.

response.end(body)

Type: <function>

Argument:

  • body

    Type: <string>

Description: Sent back the response in string.

Example Code:


                                            mimikModule.exports = function(context, request, response) {
                                              response.statusCode = 200;
                                              response.end('<h1>Hello World</h1>');
                                          };
                                        

response.writeMimeFile(filePath, mimeType)

Type: <function>

Argument:

  • filePath

    Type: <string>

  • mimeType

    Type: <string>
    Default: application/json

Description: Sent back the response as mime file.

Example Code:


                                                  mimikModule.exports = function(context, request, response) {
                                                    response.statusCode = 200;
                                                    var filepath = 'filename.jpeg';
                                                    // var fullpath = '/home/user/download/example.jpeg';
                                                    response.writeMimeFile(filepath, 'image/jpeg');
                                                };
                                              


Storage

Key-value Database

In order for the edge microservice to store something at the edge, mimik provides a very light-weight key-value database. Internal implementation is based on sqlite.

context.storage.getItem(key)

Type: <function>

Argument:

  • key

    Type: <string>

Return Type: <string>

Description: This method gets the data from the database using the key.

Example Code:


                                                        mimikModule.exports = function(context, req, res) {
                                                          var value = context.storage.getItem('key');
                                                          res.end(value);
                                                      };
                                                    

context.storage.setItem(key, value)

Type: <function>

Argument:

  • key

    Type: <string>

  • value

    Type: <string>

Description: This method sets the value of the data using a key in the database

Example Code:


                                                            mimikModule.exports = function(context, req, res) {
                                                              context.storage.setItem('key', 'value');
                                                              // ...
                                                          };
                                                        

context.storage.eachItem(callback(key, value))

Type: <function>

Argument:

  • callback()

    Type: <function>
    Argument: key <string>, value <string>

Description: This method iterates through the database and makes a callback for each database record.

Example Code:


                                                                    mimikModule.exports = function(context, req, res) {
                                                                      context.storage.eachItem((key, value) => {});
                                                                      // ...
                                                                  };
                                                            

context.storage.removeItem(key)

Type: <function>

Argument:

  • key

    Type: <string>

Description: This method removes an data entry from the database using the key

Example Code:


                                                                      mimikModule.exports = function(context, req, res) {
                                                                        context.storage.removeItem('key');
                                                                        // ...
                                                                    };
                                                                


File

In order for edgeSDK to manipulate files. mimik provides a minimal set of operations for dealing with files.

context.storage.saveFile(fileName, base64EncodedString)

Type: <function>

Argument:

  • fileName

    Type: <string>

  • base64EncodedString

    Type: <string>

Description: This method dumps a base64encoded binary data into a file.

Example Code:


                                                                            mimikModule.exports = function(context, req, res) {
                                                                              var base64 = '...';
                                                                              var filename = 'base64.jpg';
                                                                              context.storage.saveFile(filename, base64);
                                                                              // ...
                                                                          };
                                                                        

context.storage.deleteFile(fileName)

Type: <function>

Argument:

  • fileName

    Type: <string>

Description: This method deletes the file with corresponding filename.

Example Code:


                                                                              mimikModule.exports = function (context, req, res) {
                                                                                var filename = 'base64.jpg'; // only allows relative path.
                                                                                context.storage.deleteFile(filename);
                                                                                // ...
                                                                            };
                                                                            


HTTP Client

Context.HTTP

context.http.request(httpClientOption)

Type: <function>

Argument:

Description: The context.http is an instance of a http client interface.

Example Code:


                                                                                    mimikModule.exports = function(context, req, res) {
                                                                                      context.http.request(({
                                                                                          type: 'POST',
                                                                                          data: '{}',
                                                                                          url: 'http://www.example.com',
                                                                                          authorization: 'Bearer token',
                                                                                          success: (result) => {
                                                                                              res.end(result.data);
                                                                                          },
                                                                                          error: (err) => {
                                                                                              console.log(err);
                                                                                          }
                                                                                      }));
                                                                                      // ...
                                                                                  };
                                                                                


HttpClientOption

httpClientOption.type

Type: <string>

Description: The HTTP method of the request, such as ‘GET’, ‘POST’, ‘DELETE’, ‘PUT’, and etc.

httpClientOption.data

Type: <string>

Description: If the HTTP method is POST or PUT or PATCH, the data is the body of the method.

httpClientOption.url

Type: <string>

Description: The URL of the HTTP request.

httpClientOption.authorization

Type: <string>

Description: The HTTP authorization header of the request.

httpClientOption.success

Type: <function>

Argument:

  • result

    Type: <string>

Description: This is the success callback function.

httpClientOption.error

Type: <function>

Argument:

  • error

    Type: <string>

Description: This is the error callback function.


Edge

Decrypt Encrypted Edge Node JSON

context.edge.decryptEncryptedNodesJson(decryptNodesOption)

Type: <function>

Argument:

Description: This function decrypts the communication messages receive by the edgeSDK.

Example Code:


                                                                                            mimikModule.exports = function(context, req, res) {
                                                                                              context.http.request(({
                                                                                                  url: `${localMds}/nodes?clusters=linkLocal`,
                                                                                                  success: (result) => {
                                                                                                      const nodes = JSON.parse(result.data);
                                                                                                      const data = JSON.stringify(nodes.data);
                                                                                          
                                                                                                      context.edge.decryptEncryptedNodesJson({
                                                                                                          type: 'local',
                                                                                                          data: encryptedJson,
                                                                                                          success: (result) => {
                                                                                                              // return the decrypted data
                                                                                                              res.end(result.data);
                                                                                                          },
                                                                                                          error: (err) => {
                                                                                                              console.log(err.message);
                                                                                                          }
                                                                                                      });
                                                                                                  },
                                                                                                  error: (err) => {
                                                                                                      console.log(err.message);
                                                                                                  }
                                                                                              }));
                                                                                          };
                                                                                        


DecryptNodesOption

decryptNodesOption.type

Type: <string>

Description: The type indicates that the data to decode is either ‘account’ or ‘local’. Currently, if the data is obtained from account cluster, one should use ‘account’. Anything else should use ‘local’.

decryptNodesOption.token

Type: <string>

Description: The edge token that you obtained through mimik OpenID.

decryptNodesOption.data

Type: <string>

Description: The data json that you got from the GET /nodes that you would like to decode.

decryptNodesOption.success

Type: <function>

Argument:

  • result

    Type: <string>

Description: This is the success callback function.

decryptNodesOption.error

Type: <function>

Argument:

  • error

    Type: <string>

Description: This is the error callback function.


SEP Request

sepRequest(context, routing, option)

Type: <function>

Argument:

Description: This function allows one to make HTTP request to another node over SEP tunnel. Please note that this function does not actually come as a build-in API. Instead, please use the code included below.


                                                                                                          // get routing object
                                                                                                          function getRouting(node) {
                                                                                                              const route = {
                                                                                                                  nodeId: node.id,
                                                                                                                  localLinkNetworkId: node.localLinkNetworkId,
                                                                                                              };
                                                                                                          
                                                                                                              const json = JSON.stringify(route);
                                                                                                          
                                                                                                              const proxy = node.addresses.find(function(addr) {
                                                                                                                  return addr.type === 'proxy';
                                                                                                              });
                                                                                                              return {
                                                                                                                  id: Duktape.enc('base64', json),
                                                                                                                  port: proxy && proxy.routingPort,
                                                                                                                  url: proxy && proxy.url && proxy.url.href
                                                                                                              };
                                                                                                          };
                                                                                                          
                                                                                                          // main function
                                                                                                          function sepRequest(context, routing, option) {
                                                                                                              var sepHeader = '\r\nx-mimik-port: ' + routing.port +
                                                                                                                  '\r\nx-mimik-routing: ' + routing.id;
                                                                                                              option.authorization += sepHeader;
                                                                                                              return context.http.request(option);
                                                                                                          };      
                                                                                                      

BEP Request

context.edge.requestBep(requestBepOption)

Type: <function>

Argument:

Description: This function will request a BEP tunnel from edgeSDK. If BEP is not already created, this will create a new BEP. If a BEP is already created, this will just return the existing BEP.

Example Code:


                                                                                                                mimikModule.exports = function(context, req, res) {
                                                                                                                  context.edge.requestBep({
                                                                                                                      success: (result) => {
                                                                                                                          console.log(result.data);
                                                                                                                      },
                                                                                                                      error: (err) => {
                                                                                                                          console.log(new Error(err.message));
                                                                                                                      }
                                                                                                                  });
                                                                                                                  // ...
                                                                                                              };
                                                                                                            


RequestBepOption

requestBepOption.success

Type: <function>

Argument:

  • success

    Type: <string>

Description: This is the success callback function.

requestBepOption.error

Type: <function>

Argument:

  • error

    Type: <string>

Description: This is the error callback function.