aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

TypeError: undefined is not an object (evaluating 'punycode.ucs2.decode') #10314

Closed Dheeeraj closed 2 years ago

Dheeeraj commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication, Analytics

Amplify Categories

auth

Environment information

``` # Put output below this line System: OS: macOS 12.5.1 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 32.58 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 14.17.6 - ~/.nvm/versions/node/v14.17.6/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 6.14.15 - ~/.nvm/versions/node/v14.17.6/bin/npm Watchman: 2022.08.15.00 - /usr/local/bin/watchman Browsers: Chrome: 105.0.5195.102 Safari: 15.6.1 npmPackages: @babel/core: ^7.18.0 => 7.18.6 @babel/plugin-proposal-decorators: 7.18.2 => 7.18.2 @babel/plugin-proposal-optional-catch-binding: 7.16.7 => 7.16.7 (7.18.6) @babel/preset-env: ^7.16.11 => 7.18.6 @babel/runtime: ^7.18.3 => 7.18.6 @react-native-async-storage/async-storage: ^1.17.3 => 1.17.7 @react-native-community/cli-platform-ios: ^8.0.2 => 8.0.2 (7.0.1) @react-native-community/netinfo: ^9.3.0 => 9.3.0 @react-native-firebase/analytics: ^15.1.1 => 15.1.1 @react-native-firebase/app: ^15.1.1 => 15.1.1 @react-native-google-signin/google-signin: ^8.0.0 => 8.0.0 @react-navigation/bottom-tabs: ^6.3.2 => 6.3.2 @react-navigation/native: ~6.0.1 => 6.0.11 @react-navigation/native-stack: ^6.7.0 => 6.7.0 @react-navigation/stack: ~6.2.1 => 6.2.2 @rnx-kit/metro-config: ^1.2.32 => 1.2.34 @rnx-kit/metro-resolver-symlinks: 0.1.21 => 0.1.21 @storybook/addons: 6.5.8 => 6.5.8 (5.3.21) @storybook/react-native: 5.3.25 => 5.3.25 @storybook/react-native-server: 5.3.23 => 5.3.23 @types/i18n-js: 3.8.2 => 3.8.2 @types/jest: 26.0.19 => 26.0.19 @types/react: ^17 => 17.0.47 @types/react-native: 0.67.8 => 0.67.8 @types/react-test-renderer: 18.0.0 => 18.0.0 @typescript-eslint/eslint-plugin: 5.28.0 => 5.28.0 @typescript-eslint/parser: 5.28.0 => 5.28.0 Example: 0.0.1 ExampleAPI25: 0.0.1 HelloWorld: 0.0.1 accordion-collapse-react-native: ^1.1.1 => 1.1.1 amazon-cognito-identity-js: ^5.2.10 => 5.2.10 apisauce: 2.1.5 => 2.1.5 appcenter: ^4.4.4 => 4.4.4 appcenter-analytics: ^4.4.4 => 4.4.4 appcenter-crashes: ^4.4.4 => 4.4.4 aws-amplify: ^4.3.35 => 4.3.35 babel-jest: 26.6.3 => 26.6.3 babel-loader: 8.2.5 => 8.2.5 detox: 19.7.1 => 19.7.1 eslint: 8.17.0 => 8.17.0 eslint-config-prettier: 8.5.0 => 8.5.0 eslint-config-standard: 17.0.0 => 17.0.0 eslint-plugin-import: 2.26.0 => 2.26.0 eslint-plugin-n: ^15.0.0 => 15.2.4 eslint-plugin-node: 11.1.0 => 11.1.0 eslint-plugin-promise: 6.0.0 => 6.0.0 eslint-plugin-react: 7.30.0 => 7.30.0 eslint-plugin-react-native: 4.0.0 => 4.0.0 example: 0.1.0 fbjs-scripts: 3.0.1 => 3.0.1 hermes-inspector-msggen: 1.0.0 i18n-js: 3.9.2 => 3.9.2 jest: 26 => 26.6.3 jest-circus: 26 => 26.6.3 jest-environment-node: 26 => 26.6.2 jetifier: 2.0.0 => 2.0.0 (1.6.8) metro-config: 0.71.1 => 0.71.1 (0.67.0) metro-react-native-babel-preset: 0.71.1 => 0.71.1 (0.67.0) metro-source-map: 0.71.1 => 0.71.1 (0.67.0) mobx: 6.6.0 => 6.6.0 mobx-react-lite: 3.4.0 => 3.4.0 mobx-state-tree: 5.1.5 => 5.1.5 mocha: 6 => 6.2.3 patch-package: 6.4.7 => 6.4.7 postinstall-prepare: 1.0.1 => 1.0.1 prettier: 2.6.2 => 2.6.2 query-string: ^7.0.1 => 7.1.1 (6.10.1) react: 17.0.2 => 17.0.2 (16.14.0) react-devtools-core: 4.24.7 => 4.24.7 react-dom: ^17.0.2 => 17.0.2 (16.14.0) react-native: 0.68.2 => 0.68.2 react-native-android-intent-launcher: ^0.3.0 => 0.3.0 react-native-code-push: ^7.0.4 => 7.0.4 react-native-color-picker: ^0.6.0 => 0.6.0 react-native-country-picker-modal: ^2.0.0 => 2.0.0 react-native-device-info: ^10.0.1 => 10.0.1 (0.9.9, 8.7.1) react-native-gesture-handler: 2.4.2 => 2.4.2 react-native-get-random-values: ^1.8.0 => 1.8.0 react-native-keychain: 8.0.0 => 8.0.0 react-native-localize: ^2.2.2 => 2.2.2 react-native-push-notification: ^8.1.1 => 8.1.1 react-native-safe-area-context: 4.3.1 => 4.3.1 react-native-screens: 3.13.1 => 3.13.1 react-native-toast-message: ^2.1.5 => 2.1.5 react-native-url-polyfill: ^1.3.0 => 1.3.0 react-native-uuid: ^2.0.1 => 2.0.1 react-native-vector-icons: ^9.2.0 => 9.2.0 regenerator-runtime: ^0.13.4 => 0.13.9 rn-fetch-blob: ^0.12.0 => 0.12.0 ts-jest: 26 => 26.5.6 typescript: 4.7.3 => 4.7.3 validate.js: 0.13.1 => 0.13.1 webpack: 4 => 4.46.0 (5.73.0) npmGlobalPackages: appcenter-cli: 2.10.10 npm: 6.14.15 ```

Describe the bug

Recently i tried to update react native from

"react-native": "0.61.2",
"aws-amplify": "^3.3.4",
"amazon-cognito-identity-js": "^3.2.0",

to

"react-native": "0.68.2",
"aws-amplify": "^4.3.35",
 "amazon-cognito-identity-js": "^5.2.10",

In old configuration amplify pull and running project is working as expected events are being sent if i do the same in the new configuration i'm getting following error TypeError: undefined is not an object (evaluating 'punycode.ucs2.decode')

Expected behavior

Sending analytics object to AWSKinesisFirehoseProvider

Reproduction steps

Sending any record to analytics triggers the following error i'm not using any auth sign in functions( i guess its failing to sign in as Guest) since i'm trying to send events immediately after app starts

Code Snippet

import 'react-native-url-polyfill/auto';
import 'react-native-get-random-values';
import { Analytics, AWSKinesisFirehoseProvider, Amplify, Auth } from "aws-amplify"

Analytics.addPluggable(new AWSKinesisFirehoseProvider())
Amplify.configure({
  ...awsconfig,
})

//configuration of analytics
Analytics.configure({
  AWSKinesisFirehose: {
    region: REGION,
    bufferSize: 1000,
    flushSize: 100,
    flushInterval: 1000,
    resendLimit: 5,
  },
})

Log output

``` // Put your logs below this line [DEBUG] 50:32.396 Credentials - getting credentials LOG [DEBUG] 50:32.396 Credentials - picking up credentials LOG [DEBUG] 50:32.396 Credentials - getting new cred promise LOG [DEBUG] 50:32.397 Credentials - checking if credentials exists and not expired LOG [DEBUG] 50:32.397 Credentials - need to get a new credential or refresh the existing one LOG [DEBUG] 50:32.397 Credentials - no credentials for expiration check LOG [DEBUG] 50:32.398 AuthClass - Getting current user credentials LOG [Error: Exception in HostFunction: Malformed calls from JS: field sizes are different. [[28,37],[1,0],[[74,2000,1663017632404,false]],318]] LOG [DEBUG] 50:32.406 AuthClass - failed to get or parse item aws-amplify-federatedInfo [SyntaxError: JSON Parse error: Unexpected identifier "undefined"] LOG [DEBUG] 50:32.406 AuthClass - Getting current session LOG [DEBUG] 50:32.406 AuthClass - getting guest credentials LOG [DEBUG] 50:32.408 Credentials - setting credentials for guest LOG [DEBUG] 50:32.430 Credentials - Failed to load credentials {"_U": 1, "_V": 3, "_W": {"_1": 2, "_U": 0, "_V": 2, "_W": [TypeError: undefined is not an object (evaluating 'punycode.ucs2.decode')], "_X": null}, "_X": null} LOG [DEBUG] 50:32.436 Credentials - Error loading credentials [TypeError: undefined is not an object (evaluating 'punycode.ucs2.decode')] LOG [DEBUG] 50:32.436 AWSKinesisProvider - set credentials for analytics undefined LOG [DEBUG] 50:32.439 AWSKineisFirehoseProvider - init clients LOG [DEBUG] 50:32.439 AWSKineisFirehoseProvider - initialize kinesis firehose with credentials {"accessKeyId": undefined, "authenticated": undefined, "identityId": undefined, "secretAccessKey": undefined, "sessionToken": undefined} LOG [DEBUG] 50:32.442 AWSKineisFirehoseProvider - putting records to kinesis ["appAnalytics", "with records", [{"Data": [Uint8Array]}]] LOG [DEBUG] 50:32.472 AWSKineisFirehoseProvider - Failed to upload records to Kinesis [TypeError: undefined is not an object (evaluating 'punycode.ucs2.decode')] ```

aws-exports.js

awsconfig={
 "aws_project_region": "xxx",
    "aws_cognito_identity_pool_id": "xx-xx:xxxx-xx-x-xx",
    "aws_cognito_region": "xxx",
    "oauth": {},
    "aws_cognito_username_attributes": [],
    "aws_cognito_social_providers": [],
    "aws_cognito_signup_attributes": [],
    "aws_cognito_mfa_types": [],
    "aws_cognito_password_protection_settings": {
        "passwordPolicyCharacters": []
    },
    "aws_cognito_verification_mechanisms": []
}

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

Dheeeraj commented 2 years ago

Finally i was able to figure out the cause. It is because of react-native-url-polyfill which has a dependency

whatwg-url-without-unicode: "8.0.0-3"

i had to make following changes inside node_modules/whatwg-url-without-unicode/lib/url-state-machine.js

-const punycode = require("punycode");
+const punycode = require("punycode/");

//replace all
- punycode.ucs2.decode
+ punycode.ucs2decode

and patch it with patch-package

sdwvit commented 1 year ago

I wasn't too happy about patching module, thus I tried to find another way to fix it on consumer side. see: https://github.com/charpeni/whatwg-url/issues/5#issuecomment-1608591564

oksana-vyshinskaya commented 8 months ago

I just added to package.json the previous version of punycode instead of patch

"resolutions": {
    "punycode": "1.4.1"
 }