simov / grant

OAuth Proxy
MIT License
4.1k stars 257 forks source link

Noncritical errors when bundling with webpack #212

Open joeyvanlierop opened 3 years ago

joeyvanlierop commented 3 years ago

I am using the aws handler and when running using serverless-offline, I am getting the following errors:

Module not found: Error: Can't resolve 'hapi/package.json' in 'C:\...node_modules\grant'
 @ C:/.../node_modules/grant/grant.js 15:16-44 42:14-42
 @ C:/.../api/functions.ts'
ERROR in C:/.../node_modules/grant/grant.js
Module not found: Error: Can't resolve 'koa/package.json' in 'C:\...\node_modules\grant'
 @ C:/.../node_modules/grant/grant.js 15:16-44 42:14-42
 @ C:/.../api/functions.ts
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'C:\hiberfil.sys'
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'C:\pagefile.sys'
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'C:\swapfile.sys'

Here is a condensed version of the code I am using:

import { APIGatewayProxyHandler } from "aws-lambda";
import grant from "grant";

const grantHandler = grant.aws({
  config: {
    defaults: {
      origin: "http://localhost:4000",
      transport: "state",
    },
    github: {
      key: "xxx",
      secret: "xxx",
      scope: ["user"],
      redirect_uri: "http://localhost:4000/dev/connect/github/callback",
    },
  },
  session: { secret: "grant" },
});

export const login: APIGatewayProxyHandler = async (event) => {
  const { redirect, response } = await grantHandler(event);
  return redirect || createResponse(200, JSON.stringify(response));
};

Neither of the errors seem to have any impact, however I am wondering if you have noticed this before. I couldn't find any similar issues so this might just be a side-effect of using serverless-offline or something similar. If there is anything else I can do to help please let me know. Thanks and happy holidays 🎆 🎉

joeyvanlierop commented 3 years ago

The errors disappear when commenting out the following lines in the main grant.js file:

function grant ({handler, ...rest}) {
  if (handler === 'express') {
    var version = 4
  }
  // else if (handler === 'koa') {
  //   var version =
  //     parseInt(require('koa/package.json').version.split('.')[0]) >= 2 ? 2 : 1
  // }
  // else if (handler === 'hapi') {
  //   try {
  //     var pkg = require('@hapi/hapi/package.json')
  //   }
  //   catch (err) {
  //     var pkg = require('hapi/package.json')
  //   }
  //   var version = parseInt(pkg.version.split('.')[0]) >= 17 ? 17 : 16
  // }
  return version
    ? require(`./lib/handler/${handler}-${version}`)(rest)
    : require(`./lib/handler/${handler}`)(rest)
}

grant.express = (options) => {
  var version = 4
  var handler = require(`./lib/handler/express-${version}`)
  return options ? handler(options) : handler
}

// grant.koa = (options) => {
//   var version =
//     parseInt(require('koa/package.json').version.split('.')[0]) >= 2 ? 2 : 1
//   var handler = require(`./lib/handler/koa-${version}`)
//   return options ? handler(options) : handler
// }

// grant.hapi = (options) => {
//   try {
//     var pkg = require('@hapi/hapi/package.json')
//   }
//   catch (err) {
//     var pkg = require('hapi/package.json')
//   }
//   var version = parseInt(pkg.version.split('.')[0]) >= 17 ? 17 : 16
//   var handler = require(`./lib/handler/hapi-${version}`)
//   return options ? handler(options) : handler
// }

;[
  'fastify', 'curveball',
  'node', 'aws', 'azure', 'gcloud', 'vercel'
].forEach((provider) => {
  grant[provider] = (options) => {
    var handler = require(`./lib/handler/${provider}`)
    return options ? handler(options) : handler
  }
})

grant.default = grant
module.exports = grant

I am using serverless-bundle which uses serverless-webpack under the hood and I am thinking that webpack might be interacting oddly with grant. Just a thought.

simov commented 3 years ago

Thanks for the detailed bug report :+1:

Yes, it is an issue with the bundler because Grant loads optional dependencies at runtime where the bundler tries to resolve all require statements eagerly regardless of logical code structure.

Not sure if or how this will be possible to fix because by design Grant was never meant to be bundled, but lets keep this issue open for now. Probably with an updated title regarding the bundling process.

simov commented 3 years ago

I did a few improvements on the imports, now all package.json imports are wrapped in try catch blocks so they should produce warnings only. On top of that I did a few tests on my own and I was able to suppress the Webpack warnings using the following configuration:

var path = require('path')

var webpack = require('webpack')
// suppresses the warnings in the console
var ignore = new webpack.IgnorePlugin({
  resourceRegExp: /(koa|hapi)\/package\.json/
})

module.exports = {
  entry: './examples/grant.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'webpack.bundle.js',
  },
  target: 'node',
  plugins: [ignore],
  mode: 'production',
}