Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ngault opened this issue Sep 24, 2020 · 7 comments
Closed
Assignees
Labels
amazon-cognito-identity-js Used for issues related to this specific package within the monorepo

Comments

@ngault
Copy link

ngault commented Sep 24, 2020

Describe the bug
With the introduction of isomorphic-unfetch to amazon-cognito-identity-js/src/Client.js at

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.
developit/unfetch#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:
developit/unfetch#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.

@ngault ngault added the to-be-reproduced Used in order for Amplify to reproduce said issue label Sep 24, 2020
@ngault ngault changed the title Web Worker and Service Worker support broken by new cognito-identity-js Web Worker and Service Worker reliably destroyed by new cognito-identity-js Sep 25, 2020
@ngault ngault changed the title Web Worker and Service Worker reliably destroyed by new cognito-identity-js All Web- and ServiceWorkers reliably destroyed by new cognito-identity-js Sep 25, 2020
@bebraw
Copy link

bebraw commented Sep 28, 2020

For reference, it looks like supabase/postgrest-js#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.

@sammartinez sammartinez added the amazon-cognito-identity-js Used for issues related to this specific package within the monorepo label Sep 28, 2020
@ngault
Copy link
Author

ngault commented Sep 29, 2020

FYI, the offending problem in the unfetch repo has just now been fixed:
developit/unfetch#109

The problem in amazon-cognito-identity-js should go away by updating to the isomorphic-unfetch dependency, which depends on unfetch.

@mauerbac mauerbac added bug Something isn't working and removed to-be-reproduced Used in order for Amplify to reproduce said issue labels May 19, 2021
@sammartinez
Copy link
Contributor

Thanks for reporting this @ngault, @nickarocho can we look at this bug and see about the upgrade path?

@nickarocho
Copy link
Contributor

Hi @ngault, I started looking into this and was wondering if this is still an issue for you? The package.json within Amplify's amazon-cognito-identity-js package is showing the following version: "isomorphic-unfetch": "^3.0.0".

Since this should bump the dependency to any minor version (via the ^) if you do a fresh yarn install and inspect the browser.js file within the node_modules/isomorphic-unfetch directory, you should see the file contains the following fix you were hoping for:

module.exports = self.fetch || (self.fetch = require('unfetch').default || require('unfetch'));

If this is still a problem for you we will open a PR with the minor version bump, otherwise I believe we can close this issue.

Thanks!

@sammartinez sammartinez added pending-close-response-required and removed bug Something isn't working labels May 19, 2021
@ngault
Copy link
Author

ngault commented May 20, 2021

If this is still a problem for you we will open a PR with the minor version bump, otherwise I believe we can close this issue.

Thanks!

The problem was fixed within a few days of my reporting it 8 month as ago. @ericclemmons took the lead.

@nickarocho
Copy link
Contributor

Got it, thanks for the follow up!

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
amazon-cognito-identity-js Used for issues related to this specific package within the monorepo
Projects
None yet
Development

No branches or pull requests

5 participants