sodaru / aws-cognito-srp-client

SRP Client for AWS Cognito in Javascript
MIT License
1 stars 1 forks source link

import_aws_cognito_srp_client.default is not a constructor... #2

Open tvanriper opened 1 year ago

tvanriper commented 1 year ago

Following README.md directly with:

import Srp from 'aws-cognito-srp-client';

const srp = new Srp(USERPOOLID);

When I run this in my dev environment, everything works very well. Very pleased with it.

When I run this within a lambda function in AWS, an exception is thrown reading:

err: TypeError: import_aws_cognito_srp_client.default is not a constructor

I don't know if it's helpful, but lambda runs nodejs v16.12. My dev environment run v16.19.0. I have not tried changing the dev environment to an earlier version of nodejs to see if that's the difference.

tvanriper commented 1 year ago

tsc --version on my dev box gives me:

Version 4.9.5

But considering this gets compiled in my dev environment, I would think it should Just Work in Lambda. Very stumped.

tvanriper commented 1 year ago

I've submitted a pull request, but I'm still experiencing problems. Namely, when I use this in my dev environment with the modifications I've submitted in my pull request, the polyfill fails to find a native crypto.

The reason for this is best explained here:

https://codedamn.com/news/javascript/fix-require-is-not-defined

Note where it states:

The "require()" function and the import/export syntax for ES6 modules cannot be combined within a single project. One or the other must be used consistently.

So I tried modifying the cryptoUtils.ts code to have the following at the top:

import * as cryptonite from "crypto";

Then, within the polyFillCrypto function, I placed the following:

if (typeof cryptonite !== "undefined") {
  return cryptonite;
}

But then, what is the point of the polyfill? Is it possible to remove polyFillCrypto and the const crypto = polyFillCrypto(); line altogether and use this at the top of the file:

import * as crypto from "crypto";

I don't quite know enough about what this area of the code is intending to know if this is the most correct way to handle this. I'm also not familiar enough with TS/JS to know if any of this is quite right.

Raaghu commented 1 year ago

I would like to know more on how you deploy code to lambda ? are you using any frameworks or manually copying ?

tvanriper commented 1 year ago

For this code, I'm using sveltekit-adapter-aws.