azure-functions-pack icon indicating copy to clipboard operation
azure-functions-pack copied to clipboard

FuncPack does not worky with openpgpjs

Open securityvoid opened this issue 8 years ago • 3 comments
trafficstars

If you require openpgpjs in your project, you will get the error: mscorlib: Error: Cannot find module 'crypto'

This is even if you have the module "crypto" required by the application.

Repro steps

Either create the following files below, or clone this repo: https://github.com/securityvoid/funcpack-bug.git

  1. Create a simple Azure function based on an HTTP Trigger that uses the 'openpgp' library.

e.g. index.js:

'use strict';

const   openpgp     =   require('openpgp');

module.exports = function(context){
    context.done();
}

function.json

{
 "disabled": false,
 "bindings": [
  {
   "name": "req",
   "type": "httpTrigger",
   "direction": "in",
   "authLevel": "anonymous",
   "methods": [
    "GET"
   ],
   "route": "/test"
  },
  {
   "name": "res",
   "type": "http",
   "direction": "out"
  }
 ]
}
  1. Run your Azure Function locally by running the following in the directory of the host.json: func run abc

  2. Send a request to the /test URL and note you get a blank page (aka it works).

  3. Stop func so its no longer running.

  4. Run funcpack in the base folder. e.g. funcpack

  5. Repeat steps 2-3 from above.

NOTE: You now receive an error message that the module 'crypto' is not defined.

The error points to the util.js file from openpgp: https://github.com/openpgpjs/openpgpjs/blob/master/src/util.js

And specifically the following section:

/**
   * Get native Node.js crypto api. The default configuration is to use
   * the api when available. But it can also be deactivated with config.use_native
   * @return {Object}   The crypto module or 'undefined'
   */
  getNodeCrypto: function() {
    if (!this.detectNode() || !config.use_native) {
      return;
    }

    return require('crypto');
  },

Expected behavior

The application continues to work after funcpack is applied.

Actual behavior

The following error message is received:

mscorlib: Error: Cannot find module 'crypto'

Known workarounds

None found yet, welcome to suggestions!

Related information

Running on Windows 10 npm --version 3.10.8

node --version v6.9.2

funcpack --version 0.0.1

securityvoid avatar Jun 28 '17 18:06 securityvoid

@christopheranderson responded to a stack overflow post I have on this issue. The above problem is because "crypto" is a native module which therefore has to be excluded from Webpack.

The ability to ignore modules has been added in the following feature branch: https://github.com/Azure/azure-functions-pack/tree/feature-ignoremodules

I was able to test this by merging in the feature branch and confirm that it works. Thank-you @christopheranderson ! This was really bugging me and holding up a project!

Details on excluding externals can be found at the following Webpack documentation..

Specifically its done in the WebPack configuration by referencing "externals", e.g.:

debug("Creating Webpack Configuration");
            const config: webpack.Configuration = {
                entry: oldPath,
                externals: ignoredModules,
                node: {
                    __dirname: false,
                    __filename: false,
                },
                output: {
                    filename: "output.js",
                    library: "index",
                    libraryTarget: "commonjs2",
                    path: path.join(options.projectRootPath, options.outputPath),
                },
                plugins: [],
                target: "node",
            };

securityvoid avatar Jun 29 '17 13:06 securityvoid

Okay, sorry for the open and closes. I did not test well enough in the beginning.

At this stage I have the "dev" branch built, and trying it against the sample bug git repo I still get the error that it could not find the module crypto.

This is my funcpack.config.json:

{
    "ignoredModules":[
        "crypto"
    ]
}

Am I missing something else I need to do to use this? Debugging the module, it looks like the proper values are getting to "externals" part of the Webpack config.

securityvoid avatar Jun 29 '17 14:06 securityvoid

This is a weird one because it should just ignore crypto. Maybe I have my package paths being generated wrong. I'll try to repro and see what's up.

christopheranderson avatar Jul 06 '17 16:07 christopheranderson