Search code examples
ibm-cloudiconvopenwhisk

Using iconv in Bluemix's OpenWhisk


I am trying to read a non-ASCII character from a Web page from an OpenWhisk action in Bluemix. I use the following code snippet:

 function main(params) {
    var request = require('request');
    var iconv  = require('iconv-lite');
    request({ method: 'GET'
     , uri: params.url , encoding: null
     }, function(error, response, data) {
        var raw = "";
         raw = iconv.decode(buffer(data),"ISO-8859-1");

        return whisk.done({"raw": raw});
    });
    return whisk.async();
 } 

The module iconv is not found, neither does node-iconv or iconv-lite. So how would I convert the String encoding so that I can parse it?

Thank you

Budi Darmawan


Solution

  • OpenWhisk installs a small number of npm modules in the base image (which is documented here).

    A workaround to use an npm package that is not available by default is to bundle 3rd party dependencies into your javascript action. This guide provides a way to do it using a webpack: https://developer.ibm.com/openwhisk/2016/03/17/bundling-openwhisk-actions-with-webpack/

    I suggest creating an issue/feature request to add 'iconv-lite' to OpenWhisk - Since only these encodings are available by default in the node.js runtime.

    Another workaround, if you are willing to run a local OpenWhisk deployment, is to add the package in this Dockerfile for Node.js v6.2.0 or this Dockerfile for Node.js v0.12.14.

    Lastly as FYI, note that there is currently a limitation that prevents non-ASCII characters from serializing and deserializing correctly in action results (and logs). See this issue for reference: https://github.com/openwhisk/openwhisk/issues/252