question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

All Web- and ServiceWorkers reliably destroyed by new `cognito-identity-js`

See original GitHub issue

Describe the bug With the introduction of isomorphic-unfetch to amazon-cognito-identity-js/src/Client.js at https://github.com/aws-amplify/amplify-js/blob/7cbed47cb2bbbc3a94a6fcae87efee80a5542f2e/packages/amazon-cognito-identity-js/src/Client.js#L1 any Service Worker or Web Worker making API calls will crash with a window not found. Moreover, the inclusion of file in a JS bundle instantly destroys the browser’s Worker environment, with other fatal exceptions soon to follow.

This is because isomorphic-unfetch depends on unfetch, which fails to check for self (i.e., a Worker environment), and, thereby, replaces the Worker environment’s built-in fetch implementation with a fetch polyfill that, incorrectly, assumes window is present. https://github.com/developit/unfetch/issues/104

To fully appreciate the severity of this bug, consider that that the standardized worker implementation of the fetch spec (in Chrome it’s >> 100,000 lines, and includes proxying, cacheing, etc) is replaced by a 500 line polyfill the moment the worker loads the Amplify JS bundle, and executes import "isomorphic-unfetch".

Unfortunately, theunfetch project is not actively maintained, with a straightforward PR to fix the problem being outstanding almost 2 years: https://github.com/developit/unfetch/pull/109

The problem CAN be fixed without impacting any other Amplify code by either: 1. replacing unfetch with a fetch polyfill that correctly detects all environments, OR 2. checking for self on the 1st line of Client.js, and, if a Worker is detected, not importing isomorphic-fetch and overwriting the Worker global namespace


To Reproduce Based on the self-evident nature of the problem and its documentation in the isomorphic-unfetch/unfetch repo, reproducing the behavior in a Worker using Amplify should not be necessary… but, if our process nonetheless requires reproducing, do this:

From within a Web or Service Worker, call any method in @aws-amplify/auth (which will, in turn invoke amazon-cognito-identity-js/src/Client.js) or any other Amplify function dependent on @aws-amplify/auth.

The /amazon-cognito-identity-js/__tests__/ does not currently consider workers (nor SSR). Similarly, there is no testing for authentication in workers in core… all of which explains why this problem wasn’t previously identified.

To configure a worker to use Auth, you’ll need to provide an implementation of a the ICognitoStorage interface with your Config with something like:

Auth.configure({
  storage: AuthStorage,
});

The custom storage interface is required because workers don’t support LocalStorage, Amplify’s default store. Numerous examples for memory and IndexedDB stores can be found with a Google search.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bebrawcommented, Sep 28, 2020

For reference, it looks like https://github.com/supabase/postgrest-js/pull/109 moved to use cross-fetch which doesn’t appear to have the issue when run within workers so using that over the current solution is one option.

0reactions
github-actions[bot]commented, May 21, 2022

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serverless File Upload with AWS Cognito and S3 - Hipo
The simple solution is to use S3 to serve all kinds of static files. It's scalable, fast and reliable. For smaller files like...
Read more >
P-Verifier: Understanding and Mitigating Security Risks in ...
Our analysis is guided by the theories of strings and automata [8] and partially automated by P-Verifier, a formal verification tool to verify ......
Read more >
Serverless APIGateway Service Proxy - Morioh
This Serverless Framework plugin supports the AWS service proxy integration feature of API Gateway. You can directly connect API Gateway to AWS services...
Read more >
api gateway | Noise | Page 7
The client needs to issue subsequent calls to the new location in order to retrieve ... AWS has highly reliable and scalable fully-managed...
Read more >
Terms and Conditions - Whickr
4.1 To be eligible to use our Services, you must (i) have full legal capacity to enter into a contract; (ii) if you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found